0
[{name:'a',test :1},{name:'b',test :2}]

to

{ a : {test:1}, b:{test:2} }
1
  • Is this supposed to be JSON? Commented Nov 25, 2022 at 3:57

1 Answer 1

1

If your input is valid JSON, you can use the built-in functions and operators and aggregate functions:

SELECT json_object_agg(a.e ->> 'name', a.e - 'name')
FROM jsonb_array_elements('[{"name":"a","test":1},{"name":"b","test":2}]') a(e)

db<>fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Or just: SELECT json_object_agg(a.e ->> 'name', a.e - 'name') FROM jsonb_array_elements('[{"name":"a","test":1},{"name":"b","test":2}]') a(e)
@ErwinBrandstetter I'm always struggling with set-returning functions. Thanks for the improvement!

Your Answer

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.