Reputation: 65
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?
Upvotes: 1
Views: 59
Reputation: 19603
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)
Upvotes: 1