I have JSON data stored in a JSONB field in my postgresql 9.5 DB.
Is there a way of making sub-objects columns without knowing which column is a sub-object?
JSON example in question:
{
"a":1,
"b":[1,2,3],
"c":"bar",
"d":{
"key1":"value1",
"key2":"value2"
}
}
I can use the following to get all of the keys from a JSON object.
SELECT * FROM json_object_keys('{"a":1,"b":[1,2,3],"c":"bar", "d":{"key1":"value1", "key2":"value2"}}')
At that point I can then use json_to_record() but I would like to split the column out to their own separate fields.
select * from json_to_record('{"a":1,"b":[1,2,3],"c":"bar", "d":{"key1":"value1", "key2":"value2"}}') as x(a int, b text, c text, d text)
gets me
a| b | c | d
1| [1,2,3] | bar | {"key1":"value1", "key2":"value2"}
Is there a way to get something like this back, preferably in a single query?
--------------------------------------------------------------------
a| b | c | d | key1 | key2
1| [1,2,3] | bar | {"key1":"value1", "key2":"value2"} |value1 |value2