I am getting below payload as string and need to extract all the values for key "id" .
I am writing like this:
JSON_EXTRACT_SCALAR(api_response, '$.0.id') as extract_id
But it is extracting only for "0"th index leaving rest of the id value. Can someone please guide me how to extract all the ids here.
api_response:
{
"0":{
"id": 1,
"value":"xxx" ,
"author":{
"name":"some name",
"surname":"some surname"
},
},
"1":{
"id":2,
"value":"yyy",
"author":{
"name":"some name1",
"surname":"some surname1"
},
}
"3":{
"id":3,
"value":"zzz",
"author":{
"name":"some name2",
"surname":"some surname2"
},
}
}
I am getting below payload as string and need to extract all the values for key "id" .
I am writing like this:
JSON_EXTRACT_SCALAR(api_response, '$.0.id') as extract_id
But it is extracting only for "0"th index leaving rest of the id value. Can someone please guide me how to extract all the ids here.
api_response:
{
"0":{
"id": 1,
"value":"xxx" ,
"author":{
"name":"some name",
"surname":"some surname"
},
},
"1":{
"id":2,
"value":"yyy",
"author":{
"name":"some name1",
"surname":"some surname1"
},
}
"3":{
"id":3,
"value":"zzz",
"author":{
"name":"some name2",
"surname":"some surname2"
},
}
}
JSON_EXTRACT(api_response, '$.*.content')
works fine and extracts all the required id from the payload.
I don't think this is possible in Presto, but Trino (the successor project to Presto) supports the SQL standard JSON function. With Trino you can run the following:
SELECT json_query(
'{
"0":{
"id": 1,
"value":"xxx" ,
"author":{
"name":"some name",
"surname":"some surname"
}
},
"1":{
"id":2,
"value":"yyy",
"author":{
"name":"some name1",
"surname":"some surname1"
}
},
"3":{
"id":3,
"value":"zzz",
"author":{
"name":"some name2",
"surname":"some surname2"
}
}
}',
'lax $.*.id' WITH ARRAY WRAPPER);
This results in:
_col0
---------
[1,2,3]
(1 row)
Note, the example JSON you posed is invalid as it has trailing commas (after the author field), and is missign a comma after object 1
. Without fixing the invalid JSON you get null or an error.
$.*.id
as your json path expression. – gwcoffey Commented Jan 7 at 0:22$..id
. That (if it works) would grab the value of any property namedid
not just these top level objects. But if your JSON always looks like this maybe that is ok? – gwcoffey Commented Jan 7 at 0:37