Building JSON objects - Mailing list pgsql-general

From Eli Murray
Subject Building JSON objects
Date
Msg-id CAHReO_sU68GbTjhXjzA1Q+5eYdETZU5SdQ0f6yqMrP=_Y68+UQ@mail.gmail.com
Whole thread Raw
Responses Re: Building JSON objects  (Adrian Klaver <adrian.klaver@aklaver.com>)
List pgsql-general
Hi All,

I'm trying to create an array of JSON objects from an existing table. I've created a new table:

"CREATE TABLE json(data json[]);"

Now i need to populate it with key value pairs from another table. I'd like to create the objects to look like: 

{ "code": rawdata.deptcode, "name": rawdata.deptname } 

But I'm having difficulty understanding the syntax to do this because I find the docs to be rather difficult to parse.

I've tried running the query: 

"INSERT INTO json(data) json_build_object(SELECT DISTINCT deptcode, deptname AS code, name FROM rawdata);"

but it returns a syntax error.

I've also tried running:

"INSERT INTO json(data) row_to_json(SELECT DISTINCT deptcode, deptname FROM rawdata);"

but no dice.

Does anyone know the proper syntax to create an array of JSON objects from an existing table?

--
Senior Web Developer at The Daily Illini
ejmurra2@illinimedia.com
(815) 985-8760

pgsql-general by date:

Previous
From: Andrew Sullivan
Date:
Subject: Re: 9.4+ partial log-shipping possible?
Next
From: Adrian Klaver
Date:
Subject: Re: Building JSON objects