I need to transform json objects with arbitrary keys, and integer values like so
{"a":1, "sql":5}
→ {"a":{"f":1},"sql":{"f":5}}
.
I can’t figure out the correct postgres jsonb methods. I’ve set up this db fiddle to make it easy to interact.
Help is highly appreciated. Thanks in advance.
2
Answers
You can do it with a combination of
jsonb_each
andjsonb_object_agg
in a subquery:(updated fiddle)
Without a subquery, you can also directly aggregate when fetching only a single row or when grouping by a row identifier:
(adjusted fiddle)
Returns