https://www.db-fiddle.com/f/gZXz9hJRzpiEmDr7V8PXEG/0
Postgresql 10.x
Consider the following tables:
CREATE TABLE attributes (
attr TEXT
);
INSERT INTO attributes VALUES('sth1');
INSERT INTO attributes VALUES('sth2');
CREATE TABLE items (
name TEXT,
custom JSONB
);
INSERT INTO items VALUES ('A', '{"sth1": "Hello"}');
INSERT INTO items VALUES ('B', '{"sth1": "Hello", "sth2": "Okay"}');
INSERT INTO items VALUES ('C', '{"sthNOT": "Hello", "sth2": "Okay"}');
My goal is to only query the columns from the attributes
table as Json keys in the ìtems.custom
column - so the query always returns the same set of keys.
When I know the columns I would do just:
SELECT name, custom->>'sth1', custom->>'sth2' FROM items;
I would like to make this query "dynamic" - so there can be arbitrary key definition in the attributes
table.
It would also be acceptable for me, to create a new Json object in the query - only containing the keys defined in the attributes
table and the corresponding values from the items.custom
column. So merging one Json object created from the attributes
with the items.custom
data would be an option.
Is there a way to accomplish this in Postgres?