I have documents. How can I write a query with nested json field?
Query: Count value greater than 3 output: doc-1 and doc-3
Doc-1
"1": {
"count":4,
"name": "pen"
}
Doc-2
"2": {
"count":1,
"name": "eraser"
}
Doc-3
"3": {
"count":43,
"name": "book"
}
Convert the dynamic object (OBJECT_VALUES(), OBJECT_NAMES(), OBJECT_PAIRS()) into ARRAY and use ANY clause
https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/objectfun.html
SELECT b.*
FROM mybucket AS b
WHERE ANY v IN OBJECT_VALUES(b) SATISFIES v.`count` > 3 END;
Related
When I have a simple JSON like:
{
"name": "Tom",
"age": 20
}
Is there any JMESPath query to get age only when name is Tom?
The query should get 20 with the upper JSON.
But, if the name is not Tom like:
{
"name": "Bob",
"age": 31
}
The query should return null.
In order to filter, you will need an array.
And you can get an array from any object with the function to_array.
Then, because you have an unique object, you can stop the projection created by the filter, and, take the first element of the array, using | [0], as explained in the pipe expressions section of the tutorial.
So with the query:
to_array(#)[?name == `Tom`].age | [0]
This will give 20 for the JSON
{
"name": "Tom",
"age": 20
}
This will give null for the JSON
{
"name": "Bob",
"age": 31
}
I am Using MySQL 5.7 where I have below table structure:
id(bigInt)
item_name(varchar)
attributes(JSON array)
Example Data Set
1,"PRODUCT",'[ { "1": [2,4,1]},{ "2": [5,4,6]},{ "3": [5,3,2]}]'
I want to fetch the row based on the attributes field where key value is 1 (Not to include values search needs to be done on keys only).
Tried queries:
SELECT JSON_CONTAINS('{"attributes":[ { "1": [2,4,1]},{ "2": [5,4,6]},{ "3": [5,3,2]}]}',"1") Result;
This always returns 0n as result
SELECT JSON_SEARCH('{"attributes":[ { "1": [2,4,1]},{ "2": [5,4,6]},{ "3": [5,3,2]}]}', 'one', "1") Result;
This always return null
Any help will be appreciated
SELECT JSON_CONTAINS(
JSON_EXTRACT(
'{"attributes":[ { "1": [2,4,1]},{ "2": [5,4,6]},{ "3": [5,3,2]}]}',
'$.attributes[*].*'),
'1')
PS. For to understand the query execute SELECT JSON_EXTRACT(..) only.
PPS. Pay attention - the value to be found must be JSON or the value which is implicitly convertable to JSON (i.e. string and not numeric).
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=edc90bff8eb2528708581576805ea98d
SELECT JSON_SEARCH('{"attributes":[ { "1": [2,4,1]},{ "2": [5,4,6]},{ "3": [5,3,2]}]}', 'one', "1")
this always return null
Read the function description carefully - JSON_SEARCH searches for string values only (the value to be searched for is specially marked as search_str) whereas the value to be found is numeric in JSON value.
I have data thats in below format and I want to load col 'c' in json format, access elements inside 'c' and perform operations on them like count or group by. How can I do it?
Sample json
{
"a": "a_data",
"b": "b_data",
"c": {"d": 123,"e": "234","f": "2" }
}
sample spec
"flattenSpec": {
"fields": [
{
"type": "jq",
"name": "c_jq",
"expr": ".c | tojson"
}
]
}
sample query and result
select c_jq from table
result:
{"d": 123,"e": "234","f": "2" }
I want to select values from col(c->d) and perform aggregation operation on them
select c_jq->d from table
select sum(c_jq->d) from table
I have a table with a data_type of json that I need to query one of the properties inside of it.
This is what the data in the column looks like:
{
"id": 7008,
"access_links": [
{
"product_code": "PRODUCT-1",
"link": "https://some.url"
},
{
"product_code": "PRODUCT-2",
"link": "https://someOther.url"
}
],
"library_id": "2d1203db-75b3-43a5-947c-8555b48371db"
}
I need to be able to pull out and filter by the product_code nested inside of the access_links.
I can get one layer deep by using this query:
SELECT
courses.course_metadata -> 'access_links' as access_links
FROM
courses
This seems to get me into the column, but I can't query any further.
The output I receive from the query looks like:
[{"product_code":"PRODUCT-1","link":"https://some.url"},{"product_code":"PRODUCT-2","link":"https://someOther.url"}]
I've tried using the ->> and #>> operators, but they both complain about the array not starting with a {. Also worth noting that the column is a data type of JSON not JSONB, so the #> operator doesn't work.
What am I missing here?
Does this help?
select
json_array_elements (x->'access_links')->'product_code' as product_code
from
(select '{
"id": 7008,
"access_links": [
{
"product_code": "PRODUCT-1",
"link": "https://some.url"
},
{
"product_code": "PRODUCT-2",
"link": "https://someOther.url"
}
],
"library_id": "2d1203db-75b3-43a5-947c-8555b48371db"
}'::json x
) as v
;
product_code
"PRODUCT-1"
"PRODUCT-2"
I have a json column in a postgres table.
The column contains the following json data:
{
"data": {
"id": "1234",
"sites": [
{
"site": {
"code": "1",
"display": "Site1"
}
},
{
"site": {
"code": "2",
"display": "Site2"
},
"externalSite": true
},
{
"site": {
"code": "3",
"display": "Site3"
}
}
]
}
}
I need to create an update query that adds another attribute ('newAttribute' in the sample below) to all array items that have '"externalSite": true', so, after running the update query the second array element will be:
{
"site": {
"code": "2",
"display": "Site2"
},
"externalSite": true,
"newAttribute": true
}
The following query returns the array elements that need to be updated:
select * from myTable, jsonb_array_elements(data -> 'sites') sites
where sites ->'externalSite' = 'true'
What is the syntax of the update query?
Thanks
Kobi
Assuming your table is called test and your column is called data, you can update it like so:
UPDATE test SET data =
(select jsonb_set(data::jsonb, '{"data","sites"}', sites)
FROM test
CROSS JOIN LATERAL (
SELECT jsonb_agg(CASE WHEN site ? 'externalSite' THEN site || '{"newAttribute":"true"}'::jsonb
ELSE site
END) AS sites
FROM jsonb_array_elements( (data#>'{"data","sites"}')::jsonb ) as ja(site)
) as sub
);
Note that I cast the data to jsonb data as there are more functions and operators available for manipulating jsonb than plain json.
You can run the SELECT statement alone to see what it is doing, but the basic idea is to re-create the sites object by expanding it with jsonb_array_elements and adding the newAttribute attribute if externalSite exists.
This array is then aggregated with jsonb_agg and, finally, in the outer select, the sites object is replaced entirely with this newly computed version.