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: 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>, Joey Adams <joeyadams3.14159@gmail.com>, Bruce Momjian <bruce@momjian.us>, PostgreSQL-development Hackers <pgsql-hackers@postgresql.org>, Jan Wieck <janwieck@yahoo.com>
Date: 2012-01-11T13:10:17Z
Lists: pgsql-hackers

On 01/11/2012 01:18 AM, Pavel Stehule wrote:
>
> I like this patch and this feature.

I'm about to read the patch in detail - I certainly like the feature.

>
> I see only one issue - there is not functionality that helps generate
> JSON in pg.
>
> What do you think about functions: array_to_json(anyarray),
> row_to_json(any) and format_json(text, text, ...)
>

Actually, more than these, I (and at least one very interested client) 
want query_to_json, which would do something like:

    # select q2json('select $$a$$ || x as b, y as c from generate_series(1,3) x, generate_series(4,5) y');
                                                      q2json
    ---------------------------------------------------------------------------------------------------------
      [{"b":"a1","c":4},{"b":"a1","c":5},{"b":"a2","c":4},{"b":"a2","c":5},{"b":"a3","c":4},{"b":"a3","c":5}]


No doubt several variants are possible such as returning a setof json, 
one per row, instead of a single json, and allowing query parameters as 
separate arguments (maybe just using variadic functions), but probably 
for a first go just something as simple as this would meet the case.

Given the short time span available before patches must be in, I am 
prepared to work on this ASAP.

cheers

andrew