I have a Postgres addresses
table with rows such as:
id | data |
---|---|
1 | {"name": "Scott", "addresses": [{"city": "London", "street": "street 1"}, {"city": "Berlin", "street": "street 2"}]} |
2 | {"name": "Tiger", "addresses": [{"city": "Tokyo", "street": "street 3"}, {"city": "Zurich", "street": "street 4"}]} |
3 | {"name": "Foooo", "addresses": [{"city": "Paris", "street": "street 5"}]} |
where data is JSON
What query would produce the following result
id | name | city | street |
---|---|---|---|
1 | Scott | London | street 1 |
1 | Scott | Berlin | street 2 |
2 | Tiger | Tokyo | street 3 |
2 | Tiger | Zurich | street 4 |
3 | Foooo | Paris | street 5 |
It looks like json_array_elements
is relevant here but I am unsure how to proceed.
2
Answers
DBFiddle demo
EDIT: You edited your question after my reply. Here is edited version:
Dbfiddle demo
Here is a way to do it using
json_array_elements
:Demo here