1

I have a column named as userId

|userId|
|  1   |
|  2   |

when I select this column I want to get value in form of json like this:-

user:{
  id:1
}
user:{
  id:2
}

is there any json function to do this in PostgreSQL?

1 Answer 1

1

Use json_build_object:

WITH t (userid) AS (
  VALUES (1),(2)
)
SELECT json_build_object('user',json_build_object('id',userid)) 
FROM t;
   json_build_object   
-----------------------
 {"user" : {"id" : 1}}
 {"user" : {"id" : 2}}
(2 rows)
Sign up to request clarification or add additional context in comments.

2 Comments

{ "user" : 1, "user" : 1} this is the result.. not exactly what I'm looking for
@a_dog_with_no_master nested json_build_object might do the trick.. check the edit :)

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.