-
I want to create a view which contains all json objects as rows(their keys as columns) from a table which has a column with json arrays. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This doesn't work if you don't know the structure of the JSON elements beforehand, as each row needs to have the same columns and the columns of a view must be inferable without actually running the query, so you can't have the columns depend on which data is in the row. SELECT json.value->>'$.foo' foo, json.value->>'$.bar' bar, json.value->>'$.baz' baz
FROM main_table tbl
INNER JOIN json_each(tbl.json_column) json You can put this into a |
Beta Was this translation helpful? Give feedback.
This doesn't work if you don't know the structure of the JSON elements beforehand, as each row needs to have the same columns and the columns of a view must be inferable without actually running the query, so you can't have the columns depend on which data is in the row.
You can use
json_each
to use a JSON array in aFROM
clause, so if you know the structure (e.g. if all the objects havefoo
,bar
,baz
fields), you can do this:You can put this into a
CREATE VIEW
statement to use it as a view.