I have a json in bigQuery
{
"actors": {
"stooges": [
{
"id": 1,
"name": "Larry"
},
{
"id": 2,
"name": "Curly"
},
{
"id": 3,
"name": "Moe"
}
]
}
}
How do I extract each of the .names in bigQuery.
["Larry", "Curly", "Moe"]
Here are some handy bigQuery compatible statements(based on above json).
-- Declaring a bigQuery variable
DECLARE json_data JSON DEFAULT (SELECT PARSE_JSON('{ "actors": {"stooges": [{"id": 1,"name": "Larry"},{"id": 2,"name": "Curly"},{"id": 3,"name": "Moe"}]}}'));
-- Select statement. But this is no good for my use case since I don't want to specify element index ([0]) as the array size is dynamic
SELECT JSON_EXTRACT(json_data, '$.actors.stooges[0].name');
You may try and consider below approach using JSON_EXTRACT_ARRAY() then unnest it and then use JSON_EXTRACT_SCALAR() to extract the values. You may refer to this documentation for more details in BigQuery JSON functions.
select ARRAY(
SELECT JSON_EXTRACT_SCALAR(json_array, '$.name') from UNNEST(JSON_EXTRACT_ARRAY(json_data,"$.actors.stooges"))json_array
)extracted_names
Output:
Related
I have a json column that holds an array of objects containing a 'name' attribute. I'm trying to build a query that can extract the unique names out of all the objects, across all rows.
Here is what the data looks like:
[{
"date": "2022-06-14T12:51:24.424Z",
"name": "review_1"
}]
[{
"date": "2022-06-14T12:50:56.454Z",
"name": "review_3"
}, {
"date": "2022-06-14T12:51:10.695Z",
"name": "review_6"
}]
[{
"date": "2022-06-14T12:51:57.997Z",
"name": "review_3"
}]
[{
"date": "2022-06-14T12:52:17.442Z",
"name": "review_1"
}, {
"date": "2022-06-14T12:54:35.239Z",
"name": "review_9
}]
My approach is to get all the names as individual rows like this so I can find the distincts.
name || date
review_1 2022-06-14T12:51:24.424Z
review_3 2022-06-14T12:50:56.454Z
review_6 2022-06-14T12:51:10.695Z
review_3 2022-06-14T12:51:57.997Z
review_1 2022-06-14T12:52:17.442Z
review_9 2022-06-14T12:54:35.239Z
I've tried using jsonb_array_elements following this post postgres + jsonb + get values of key from multidimentional array but it looks like that is for multidimentional arrays and I cannot get it to work.
It is hard to guess what kind of problems you have encountered, but the use of the function seems quite simple.
select elem->>'name' as "name", elem->>'date' as "date"
from my_table
cross join jsonb_array_elements(json_col) as arr(elem);
Test it in Db<>fiddle.
I have JSON array value column in database:
[
{
"id": 1,
"name": "One"
},
{
"id": 2,
"name": "Two"
}
]
I want to add a new key-value into each object. Expected result is:
[
{
"id": 1,
"name": "One",
"active": "1"
},
{
"id": 2,
"name": "Two",
"active": "1"
}
]
By using JSON_MODIFY I can add a new key-value into one of the object as following:
UPDATE MyTable
SET JsonValueColumn=JSON_MODIFY(JsonValueColumn,'$[1].active','1')
WHERE Id = 'id'
But I need to add a new key-value into all objects in JSON array. I have an option to iterate in json objects, add new key-value, combine them again and update JsonValueColumn with the new value.
But I am looking if there is a simpler way to achieve this. Something like:
UPDATE MyTable
SET JsonValueColumn=JSON_MODIFY(JsonValueColumn,'$[*].active','1')
WHERE Id = 'id'
This is the simplest way I found so far:
UPDATE MyTable
SET JsonValueColumn= '[' + (
SELECT
STRING_AGG(JSON_MODIFY([value],'$.active', '1'), ',') WITHIN GROUP (ORDER BY CAST([key] AS int))
FROM
OPENJSON(JsonValueColumn)
) + ']'
From: https://stackoverflow.com/a/62648430/2794280
You can use OPENJSON to break out the JSON objects, then CROSS APPLY the new value, then rebuild with FOR JSON:
UPDATE MyTable
SET JsonValueColumn =
(SELECT *
FROM OPENJSON(#json) WITH (id int, name varchar(100))
CROSS APPLY (VALUES(1)) v(active)
FOR JSON PATH);
I need to write a SQL query in the CosmosDB query editor, that will fetch results from JSON documents stored in Collection, as per my requirement shown below
The example JSON
{
"id": "abcdabcd-1234-1234-1234-abcdabcdabcd",
"source": "Example",
"data": [
{
"Laptop": {
"New": "yes",
"Used": "no",
"backlight": "yes",
"warranty": "yes"
}
},
{
"Mobile": [
{
"order": 1,
"quantity": 2,
"price": 350,
"color": "Black",
"date": "07202019"
},
{
"order": 2,
"quantity": 1,
"price": 600,
"color": "White",
"date": "07202019"
}
]
},
{
"Accessories": [
{
"covers": "yes",
"cables": "few"
}
]
}
]
}
Requirement:
SELECT 'warranty' (Laptop), 'quantity' (Mobile), 'color' (Mobile), 'cables' (Accessories) for a specific 'date' (for eg: 07202019)
I've tried the following query
SELECT
c.data[0].Laptop.warranty,
c.data[1].Mobile[0].quantity,
c.data[1].Mobile[0].color,
c.data[2].Accessories[0].cables
FROM c
WHERE ARRAY_CONTAINS(c.data[1].Mobile, {date : '07202019'}, true)
Original Output from above query:
[
{
"warranty": "yes",
"quantity": 2,
"color": "Black",
"cables": "few"
}
]
But how can I get this Expected Output, that has all order details in the array 'Mobile':
[
{
"warranty": "yes",
"quantity": 2,
"color": "Black",
"cables": "few"
},
{
"warranty": "yes",
"quantity": 1,
"color": "White",
"cables": "few"
}
]
Since I wrote c.data[1].Mobile[0].quantity i.e 'Mobile[0]' which is hard-coded, only one entry is returned in the output (i.e. the first one), but I want to have all the entries in the array to be listed out
Please consider using JOIN operator in your sql:
SELECT DISTINCT
c.data[0].Laptop.warranty,
mobile.quantity,
mobile.color,
c.data[2].Accessories[0].cables
FROM c
JOIN data in c.data
JOIN mobile in data.Mobile
WHERE ARRAY_CONTAINS(data.Mobile, {date : '07202019'}, true)
Output:
Update Answer:
Your sql:
SELECT DISTINCT c.data[0].Laptop.warranty, mobile.quantity, mobile.color, accessories.cables FROM c
JOIN data in c.data JOIN mobile in data.Mobile
JOIN accessories in data.Accessories
WHERE ARRAY_CONTAINS(data.Mobile, {date : '07202019'}, true)
My advice:
I have to say that,actually, Cosmos DB JOIN operation is limited to the scope of a single document. What possible is you can join parent object with child objects under same document. Cross-document joins are NOT supported.However,your sql try to implement mutiple parallel join.In other words, Accessories and Mobile are hierarchical, not nested.
I suggest you using stored procedure to execute two sql,than put them together. Or you could implement above process in the code.
Please see this case:CosmosDB Join (SQL API)
Please,
How can I use a query with Peewee to return a JSON format with Nested objects?
Example:
[
{
"active": 1,
"id": 1,
"name": "Los Angeles",
"idState": 2,
"TBState": {
"id": 2,
"name": "California"
}
}
]
I can it using SqlAlchemy using relationship by model but in Peewee I don't know as make.
Thanks!
Peewee has a helper model_to_dict docs that you can use.
Hi currently I want to sort array of object, I use ARRAY_SORT function, it will use the first field of object to sort & it work well if every object has the same JSON structure. If one element in array has different JSON structure, the result is incorrect.
The query I use :
SELECT ARRAY_SORT(c.student) as student FROM Class c
Result :
"student": [
{
"id": 3,
"name": "Kenny35"
},
{
"id": 6,
"name": "Kenny35"
},
{
"id": 7,
"name": "Kenny35"
},
{
"id": 8,
"name": "Kenny35"
},
{
"hobby": "video game",
"id": 5,
"name": "Kenny35"
}
]
How can I specify property of object in array for ARRAY_SORT function ?
dev,
Objects are first compared by length/size of the object, then followed by fields in the object.
http://developer.couchbase.com/documentation/server/4.5/n1ql/n1ql-language-reference/comparisonops.html
That is the only collation supported now.
-Prasad
You can issue a query and use ORDER BY.
SELECT *
FROM Class c
UNNEST c.student s
ORDER BY ...