I have some JSON data stored in a column. I want to parse the json data and extract all the values against a particular key.
Here's my sample data:
{
"fragments": [
{
"fragments": [
{
"fragments": [
{
"fragments": [],
"fragmentName": "D"
},
{
"fragments": [],
"fragmentName": "E"
},
{
"fragments": [],
"fragmentName": "F"
}
],
"fragmentName": "C"
}
],
"fragmentName": "B"
}
],
"fragmentName": "A"
}
Expected output:
D, E, F, C, B, A
I want to extract all fragmentName values from the above JSON.
I have gone through the below stacks, but haven't found anything useful: Collect Recursive JSON Keys In Postgres Postgres recursive query with row_to_json
Edited:
Here's one approach I have tried on the above stacks:
WITH RECURSIVE key_and_value_recursive(key, value) AS (
SELECT
t.key,
t.value
FROM temp_frg_mapping, json_each(temp_frg_mapping.info::json) AS t
WHERE id=2
UNION ALL
SELECT
t.key,
t.value
FROM key_and_value_recursive,
json_each(CASE
WHEN json_typeof(key_and_value_recursive.value) <> 'object' THEN '{}' :: JSON
ELSE key_and_value_recursive.value
END) AS t
)
SELECT *
FROM key_and_value_recursive;
Getting only 0 level nesting.