0

I have a table which looks like this:

Name | Data
-------------
Test1 | {"toasts": [{"type": "dark", "calories": "100"}, {"type": "white", "calories": "200"}, {"type": "gray", "calories": "300"}]}
"Test2" | {"toasts": [{"type": "white", "calories": "200"}]}

Data is an array of objects.

What I need is to select all array items in all rows:

Res
--------
{type: 'dark', calories: '100'}
{type: 'white', calories: '200'}
{type: 'gray', calories: '300'}
{type: 'white', calories: '200'}

Can anyone help?

1 Answer 1

1

Your example is not a valid json data. It should rather be :

Name Data
Test1 {"toasts": [{"type": "dark", "calories": 100}, {"type": "white", "calories": 200}, {"type": "gray", "calories": 300}]}
Test2 {"toasts": [{"type": "white", "calories": 200}]}

The query to get the expected result is :

SELECT jsonb_path_query(Data :: jsonb, '$.toasts[*]') AS res FROM my_table

see dbfiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you thats it. sorry JSON, thats not an actual prod data, I compose it on the fly. will edit now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.