1

I am trying to form a jsonb object (not array) from the table (t1) below

id key value
1 hello world
1 dns 192.2.8.0
1 people 1000

I have tried

SELECT 
 jsonb_agg(('{"' || key || '" : "' || value || '"}')::jsonb) as kv_pair 
FROM t1 GROUP BY id

This yields [{"hello":"world"}, {"dns":"192.2.8.0"}, {"people":1000}]

How do I flatten/ concatenate the array so the output is just a single object like so:

{"hello":"world" , "dns":"192.2.8.0" , "people":1000}

It is important that I have a single object for an Alembic migration.

Thanks in advance

mustaccio
28.6k24 gold badges60 silver badges77 bronze badges
asked Mar 15, 2022 at 23:21

1 Answer 1

1

turns out I was overcomplicating things and using the wrong function

SELECT id, jsonb_object_agg(key, value) as kv_pair
FROM t1 
GROUP BY id
answered Mar 16, 2022 at 12:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.