Re: JSON for PG 9.2

Andrew Dunstan <andrew@dunslane.net>

From: Andrew Dunstan <andrew@dunslane.net>
To: Pavel Stehule <pavel.stehule@gmail.com>
Cc: Joey Adams <joeyadams3.14159@gmail.com>, Robert Haas <robertmhaas@gmail.com>, "David E. Wheeler" <david@kineticode.com>, Claes Jakobsson <claes@surfar.nu>, Dimitri Fontaine <dimitri@2ndquadrant.fr>, Merlin Moncure <mmoncure@gmail.com>, Magnus Hagander <magnus@hagander.net>, Jan Urbański <wulczer@wulczer.org>, Simon Riggs <simon@2ndquadrant.com>, Bruce Momjian <bruce@momjian.us>, PostgreSQL-development Hackers <pgsql-hackers@postgresql.org>, Jan Wieck <janwieck@yahoo.com>
Date: 2012-01-15T16:08:05Z
Lists: pgsql-hackers

Attachments


On 01/14/2012 03:06 PM, Andrew Dunstan wrote:
>
>
>
> OK, here's a patch that does both query_to_json and array_to_json, 
> along with docs and regression tests. It include Robert's original 
> patch, although I can produce a differential patch if required. It can 
> also be pulled from <https://bitbucket.org/adunstan/pgdevel>
>
>


Here's an update that adds row_to_json, plus a bit more cleanup. Example:


    andrew=# SELECT row_to_json(q)
    FROM (SELECT $$a$$ || x AS b,
              y AS c,
              ARRAY[ROW(x.*,ARRAY[1,2,3]),
                    ROW(y.*,ARRAY[4,5,6])] AS z
           FROM generate_series(1,2) x,
                generate_series(4,5) y) q;
                                 row_to_json
    --------------------------------------------------------------------
      {"b":"a1","c":4,"z":[{"f1":1,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
      {"b":"a1","c":5,"z":[{"f1":1,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
      {"b":"a2","c":4,"z":[{"f1":2,"f2":[1,2,3]},{"f1":4,"f2":[4,5,6]}]}
      {"b":"a2","c":5,"z":[{"f1":2,"f2":[1,2,3]},{"f1":5,"f2":[4,5,6]}]}
    (4 rows)


(This might be more to Robert's taste than query_to_json() :-) )

cheers

andrew