0

I have a table for posts where postedBy column have relation with id from profiles table. When I am joining the tables I am getting flat object for joined data like below.

{
 id : 1,    
 postContent: "This is a post",
 postImgUrl: "https://via.placeholder.com/150",
 name : "User",
 email : "user@email.com"
}

Result I want :

{
 id : 1,    
 postContent: "This is a post",
 postImgUrl: "https://via.placeholder.com/150",
 postedBy : {
  name : "User",
  email : "user@email.com"
 }
}

Posts Table

id postContent postImgUrl postedBy
1 row https://via.placeholder.com/150 U1

Users Table

id name email
U1 User user@email.com
Laurenz Albe
  • 167,868
  • 16
  • 137
  • 184
  • 2
    Postgres has decent JSON support, the representation you want to achieve could be built with `json_to_object` or `row_to_json` functions: https://stackoverflow.com/questions/13227142/using-row-to-json-with-nested-joins – Savva Surenkov Feb 16 '22 at 11:48

0 Answers0