I have this table:
CREATE TABLE user_stats (username varchar, metadata_by_topic json);
INSERT INTO user_stats VALUES ('matt', '{"tech":["foo","bar"],"weather":"it is sunny"}');
INSERT INTO user_stats VALUES ('fred', '{"tech":{"stuff":"etc"},"sports":"bruins won"}');
The top-level keys in metadata_by_topic are always strings (e.g. "tech", "weather"), but the values under them are arbitrary json. I'd like a query that maps these top-level keys to their own column, and the json values to a different column, like so:
username | topic | metadata
-----------------------------------
matt | tech | ["foo","bar"]
matt | weather | "it is sunny"
fred | tech | {"stuff":"etc"}
fred | sports | "bruins won"
where username and topic are both of type VARCHAR and metadata is of type JSON. This:
select * from json_each((select t.metadata_by_topic from user_stats as t));
only works if I add LIMIT 1 the inner select, but that's not what I want.