a_dog_with_no_master
a_dog_with_no_master

Reputation: 65

Generating JSON in PostgreSQL

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

Answers (1)

Jim Jones
Jim Jones

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

Related Questions