Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

postgres to json object

In postgresql I have a table named info that has columns like so:

kod_id, region

I need to get a json response like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"List of regions: [
            {
                "Region name": "Moscow region",
                "Region Code": "77"
            },
            {
                "Region Name": "Belgorod Oblast",
                "Region Code": "31"
            },
            {
                "Region Name": "Bryansk Oblast",
                "Region Code": "32"
            },
            {
                "Region Name": "Vladimir Oblast", 
                                "Region Code": "33"  
            }
        ]

I wrote a query of the form:

SELECT json_build_object('List of regions' , json_build_array ('Region name', 'region', 'region', 'region code', 'kod_id') ) FROM "regions"

and got the output:

{ "List of regions" : ["Region name", "Republic of Bashkortostan", "Region code", " 02"]}
{ "List of regions" : ["Region Name", "Republic of Buryatia", "Region Code", "03"]}
{ "List of Regions" : ["Region Name", "Republic of Altai", "Region Code", "04"]}
{ "List of Regions" : ["Region Name", "Republic of Dagestan", "Region Code", "05"]}
{ "List of Regions" : ["Region Name", "Republic of Ingushetia", "Region Code", "06"]}
{ "List of Regions" : ["Region Name", "Kabardino-Balkarian Republic", "Region Code", "07"]}

Can you tell me how to get the right answer?

>Solution :

Assuming you have this data :

create table regions (
  region varchar(20),
  kod_id int
);

insert into regions values
('Moscow region', 77),
('Belgorod Oblast', 31),
('Bryansk Oblast', 32),
('Vladimir Oblast', 33);

You will need to aggregates JSON objects built by json_build_object, as a JSON array using json_agg :

SELECT json_build_object('List of regions',
                            json_agg(
                               json_build_object ('Region name', region, 'region code', kod_id)
                            )
                        )
FROM regions;

Result :

{
   "List of regions":[
      {
         "Region name":"Moscow region",
         "region code":77
      },
      {
         "Region name":"Belgorod Oblast",
         "region code":31
      },
      {
         "Region name":"Bryansk Oblast",
         "region code":32
      },
      {
         "Region name":"Vladimir Oblast",
         "region code":33
      }
   ]
}

Demo here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading