Re: JSON output

From: Craig Ringer <craig(at)postnewspapers(dot)com(dot)au>
To: Dennis Gearon <gearond(at)sbcglobal(dot)net>, PG-General Mailing List <pgsql-general(at)postgresql(dot)org>
Subject: Re: JSON output
Date: 2010-09-13 03:57:56
Message-ID: 4C8DA144.10109@postnewspapers.com.au
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 13/09/10 10:36, Dennis Gearon wrote:
> I'm trying to import from a postgres database (which will work in parallel) to a ElasticSearch databse (JSON input).
>
> Is there anyway to get JSON output from postgres?

Not out of the box.

The closest you'll get, AFAIK, is XML output from the SQLXML functions.

http://www.postgresql.org/docs/current/static/datatype-xml.html
http://www.postgresql.org/docs/current/static/functions-xml.html

In particular, table_to_xml, query_to_xml or cursor_to_xml will probably
be helpful. Once you have the XML, you can use any existing tool for an
XML-to-JSON transform, possibly in a plpython/plperl stored procedure
within PostgreSQL its self.

classads=> select * FROM table_to_xml( 'ad_status'::regclass, true,
false, '');

table_to_xml
-------------------------------------------------------------------
<ad_status xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

<row>
<id>0</id>
<name>OK</name>
</row>

<row>
<id>1</id>
<name>HELD</name>
</row>

<row>
<id>2</id>
<name>CANCELLED</name>
</row>

</ad_status>

If you need something more complex than table contents or the end result
of a query, you'll have to roll your own. There isn't anything to do
ORM-like extraction of join results into nested sets if you need that.

--
Craig Ringer

Tech-related writing: http://soapyfrogs.blogspot.com/

In response to

  • JSON output at 2010-09-13 02:36:27 from Dennis Gearon

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Craig Ringer 2010-09-13 04:01:35 Re: Help! pg_dump: Error message from server: ERROR: cache lookup failed for type 19
Previous Message Tom Lane 2010-09-13 03:33:06 Re: Change the UI language of psql connection