1

I started exploring json in postgresql. I have below jsonb in table.

'{
   "Owner":[
      {
         "Edgeid":4,
         "Weight":40,
         "EdgeColor":"Black"
      },
      {
         "Edgeid":1,
         "Weight":10,
         "EdgeColor":"Black"
      },
      {
         "Edgeid":2,
         "Weight":20,
         "EdgeColor":"Black"
      }
   ],
   "Supporter":[
      {
         "Edgeid":3,
         "Weight":30,
         "EdgeColor":"Red"
      }
   ]
}'

i am using below code to unnest "Owner" & "Supporter" into different columns

SELECT  column_name::JSONB -> 'Owner' AS owner_property, 
        column_name::JSONB -> 'Supporter' AS supporter_property 
        FROM table_name;

now suppose if i need to nesting it back to normal like above mentioned json from the two separate column. what would be my code.

Thanks in Advance.

VBAGuy
  • 172
  • 1
  • 9
  • What exactly is the output you want? –  Jul 10 '20 at 16:12
  • @a_horse_with_no_name..I need to form same json as i have given in question from column owner_property & supporter_property. – VBAGuy Jul 10 '20 at 16:41
  • json_build_object is the function we have to use but not sure exactly the code – VBAGuy Jul 10 '20 at 16:49
  • I got it. select json_build_object('Owner',owner_property,'Supporter',supporter_property) from table_name – VBAGuy Jul 10 '20 at 16:58

0 Answers0