Imagine a super simple table such as:
create table object (
id INT,
data jsonb
);
With some data:
INSERT INTO object (id, data) VALUES
(4, '{"nodes":{"f":{"id":1}}}'), -- C
(5, '{"nodes":{"d":{"id":2}, "e":{"id":3}}}'), -- B
(6, '{"nodes":{"b":{"id":4}, "c":{"id":5}}}') -- A
;
I'd like to destructure the JSON and also query for children.
For example, if I do
SELECT * FROM jsonb_each('{"a":{"id":1},"b":{"id":2}}'::JSONB) as obj
I will get back:
a {"id":1}
b {"id":2}
I'm trying to combine this to get id properties out of the nested objects and query for children (with no luck):
SELECT
jsonb_each(data->'nodes')
FROM objects as objs
WHERE id=6
LATERAL (SELECT * FROM objects as ref WHERE ref.id = objs->'id');
I've provided an SQL fiddle if it helps: http://sqlfiddle.com/#!17/50fb2/9
EDIT:
here's an example output:
id data
4 '{"nodes":{"f":{"id":1}}}'
5 '{"nodes":{"d":{"id":2}, "e":{"id":3}}}'
Thanks again for any insight into this!