Append data to JSON array in Redshift - json

I have a Redshift table in which one of the columns is a JSON array. I would like to append some data into that array. Eg:
id | col1 | col2
1 | A | {"key": []}
2 | B | {"key": []}
3 | B | {"key": ['A']}
4 | B | {"key": ['A', 'B']}
I would like to create a statement like UPDATE <table> SET col2 = <something> where col1 = 'B' so that I get:
id | col1 | col2
1 | A | {"key": []}
2 | B | {"key": ['C']}
3 | B | {"key": ['A', 'C']}
4 | B | {"key": ['A', 'B', 'C']}

You'd have to write your own User Defined Function (UDF), passing in the current value of the column and the element you would like to add, then passing back the result.
Hwoever, you really should avoid JSON columns in Amazon Redshift if at all possible. They cannot take advantage of all the features that make Redshift great (columnar, SORTKEY, etc). Plus, you'll have problems like this that are not in the normal realm of a relational database.

Related

JSON_CONTAINS() with an array of JSON objects in MySQL

This is a sample database 'test' with a JSON column 'arr' containing an array of JSON objects
+----+----------------------------------------------------------+
| id | arr |
+----+----------------------------------------------------------+
| 1 | [{"name": "aman"}, {"name": "jay"}] |
| 2 | [{"name": "yash"}, {"name": "aman"}, {"name": "jay"}] |
+----+----------------------------------------------------------+
I want to use JSON_CONTAINS to know if a value exists in a specific key of an object in the array.
Here's my query :
SELECT JSON_CONTAINS(arr, '"jay"', '$[*].name') from test WHERE id=1;
I get the following error:
ERROR 3149 (42000): In this situation, path expressions may not contain the * and ** tokens or an array range.
I know that I can try using JSON_EXTRACT() for this, but what am I doing wrong here ?
Is there any way to use JSON_CONTAINS with an array of JSON objects in MySQL.
Yes, it is possible using the following syntax:
SELECT JSON_CONTAINS(arr, '{"name": "jay"}') from test WHERE id=1;
db<>fiddle demo
Example:
+-----+--------------------------------------------------------+---+
| id | arr | r |
+-----+--------------------------------------------------------+---+
| 1 | [{"name": "aman"}, {"name": "jay"}] | 1 |
| 2 | [{"name": "yash"}, {"name": "aman"}, {"name": "jay"}] | 1 |
| 3 | [{"name": "yash"}, {"name": "aman"}] | 0 |
+-----+--------------------------------------------------------+---+
You must use JSON_SEARCH:
SELECT JSON_SEARCH(arr, 'one', 'jay', NULL, '$[*].name') IS NOT NULL
FROM test
WHERE id=1;

Azure Data Factory - Merge 2 JSON string columns into a new column

In Azure Data Factory, I have a Delimited Text source data which contains 2 JSON String columns.
I need to merge these 2 columns into a new JSON string column.
Tried Copy data but it supports only 1 to 1 mapping. Then took a look in Derived Column of Data flow, but it only has concat function to pad 2 strings.
For example I have my source data like:
| a | b |
|--------------|--------------|
| [{"foo": 0}] | [{"bar": 1}] |
| [{"baz": 2}] | |
And I want to merge Column a and b into c:
| c |
|--------------------------|
| [{"foo": 0}, {"bar": 1}] |
| [{"baz": 2}] |
Is there anyway to do this in Data Factory?

How to check for ANY like elements in JSON array in MySQL

I have a row in table A of JSON array of integers.
I have a table B whose rows have a field of a (possibly empty) JSON array of objects, and each object has a 'parent' field.
table A
+-----------+
| foo |
+-----------+
| [1, 2] |
+-----------+
table B
+--------------------------------+
| bar |
+--------------------------------+
| [{"parent": 1}] |
| [{"parent": 2}, {"parent": 3}] |
| [{"parent": 4}] |
| [] |
+--------------------------------+
How do I get the rows from table B whose objects in bar don't have any parent matching any element in foo? i.e. the last 2 rows here should be returned.
I tried doing something with SELECT JSON_EXTRACT(bar, '$[*].parent') FROM B, but the result is a JSON array
+-----------------------------------------+
| SELECT JSON_EXTRACT(bar, '$[*].parent') |
+-----------------------------------------+
| [1] |
| [2, 3] |
| [4] |
| NULL |
+-----------------------------------------+
and the function JSON_CONTAINS() doesn't return partial matches:
"A candidate array is contained in a target array if and only if every element in the candidate is contained in some element of the target." MySQL docs
Any help is greatly appreciated. MySQL version 5.7.22

Postgres update JSON field

I've got several Postgres 9.4 tables that contain data like this:
| id | data |
|----|-------------------------------------------|
| 1 | {"user": "joe", "updated-time": 123} |
| 2 | {"message": "hi", "updated-time": 321} |
I need to transform the JSON column into something like this
| id | data |
|----|--------------------------------------------------------------|
| 1 | {"user": "joe", "updated-time": {123, "unit":"millis"}} |
| 2 | {"message": "hi", "updated-time": {321, "unit":"millis"}} |
Ideally it would be easy to apply the transformation to multiple tables. Tables that contain the JSON key data->'updated-time' should be updated, and ones that do not should be skipped. Thanks!
You can use the || operator to merge two jsonb objects together.
select '{"foo":"bar"}'::jsonb || '{"baz":"bar"}'::jsonb;
= {"baz": "bar", "foo": "bar"}

Clean way to query complex JSON in Postgresql

I have JSON data stored in a JSONB field in my postgresql 9.5 DB.
Is there a way of making sub-objects columns without knowing which column is a sub-object?
JSON example in question:
{
"a":1,
"b":[1,2,3],
"c":"bar",
"d":{
"key1":"value1",
"key2":"value2"
}
}
I can use the following to get all of the keys from a JSON object.
SELECT * FROM json_object_keys('{"a":1,"b":[1,2,3],"c":"bar", "d":{"key1":"value1", "key2":"value2"}}')
At that point I can then use json_to_record() but I would like to split the column out to their own separate fields.
select * from json_to_record('{"a":1,"b":[1,2,3],"c":"bar", "d":{"key1":"value1", "key2":"value2"}}') as x(a int, b text, c text, d text)
gets me
a| b | c | d
1| [1,2,3] | bar | {"key1":"value1", "key2":"value2"}
Is there a way to get something like this back, preferably in a single query?
--------------------------------------------------------------------
a| b | c | d | key1 | key2
1| [1,2,3] | bar | {"key1":"value1", "key2":"value2"} |value1 |value2
WITH t(v) AS ( VALUES
('{
"a":1,
"b":[1,2,3],
"c":"bar",
"d":{
"key1":"value1",
"key2":"value2"
}
}'::JSONB)
)
SELECT x1.*,x2.* FROM t,
jsonb_to_record(v) as x1(a int,b text,c text,d jsonb),
jsonb_to_record(v->'d') as x2(key1 text,key2 text);
Result:
a | b | c | d | key1 | key2
---+-----------+-----+--------------------------------------+--------+--------
1 | [1, 2, 3] | bar | {"key1": "value1", "key2": "value2"} | value1 | value2
(1 row)