I have a json field (stored as text) in a table and I want to extract certain items from the json that have a specified ID in a nested array (within the json).
Each json item has a type
attribute and a value
attribute. The value
furthermore has a GRI
attribute (array) which I would like to use to filter. Here is a sample of one streamfield column item:
[{"type": "paragraph",
"value": {"paragraph": "<p>Sample paragraph 91</p>", "GRI": [27, 91, 211]},
{"type": "paragraph",
"value": {"paragraph": "<p>Sample paragraph 191</p>", "GRI": [13, 191, 271]}]
This query sort of works:
SELECT value FROM "sampletable" r, json_array_elements(r.streamfield::json) obj
WHERE obj->>'type' = 'paragraph' AND obj#>>'{value,GRI}' LIKE '%91%';
This would give me all items with ID 91. But also with ID 191, 291 etc...
So how can I convert the `obj#>>'{value,GRI}' statement into an array so that I can filter based on the specified ID?
Here is an SQLFiddle for this: http://sqlfiddle.com/#!15/184e1/1
Edit: clarified structure of JSON