I need a SQL query where I can select all json keys. The following query let's me get all the keys to the JSON field. But I'm a bit at loss how I would go about making a query to also get all the values out too.
SELECT DISTINCT ON (key.*) key.*
FROM my_table,
jsonb_object_keys(my_table.json_field) as key
So the result of the above query would simply just be
key1
key2
With the following query you would get a result similar to this
SELECT * FROM my_table
| id | json_field |
| -- | ---------- |
| 1 | '{"key1": "value1"}' |
| 2 | '{"key2": "value2"}' |
The result I'm looking for would be the following
| id | key1 | key2 |
| -- | -------| ------ |
| 1 | value1 | null |
| 2 | null | value2 |
What makes it difficult is that I don't know the names of all keys which also may be a lot of keys for a single row.