Re: Building JSON objects

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Eli Murray <ejmurra2(at)illinimedia(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: Building JSON objects
Date: 2015-03-27 17:47:54
Message-ID: 551597CA.90607@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 03/27/2015 10:40 AM, Eli Murray wrote:
> 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.

That would be?

What version of Postgres?

>
> 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?

json_build_array?
http://www.postgresql.org/docs/9.4/interactive/functions-json.html

>
> --
> Senior Web Developer at The Daily Illini
> ejmurra2(at)illinimedia(dot)com <mailto:ejmurra2(at)illinimedia(dot)com>
> (815) 985-8760

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Arup Rakshit 2015-03-27 18:00:54 JSON merge in postgresql
Previous Message Eli Murray 2015-03-27 17:40:11 Building JSON objects