On Mar 13, 2013, at 10:59 AM, Andrew Dunstan <[email protected]> wrote:
>> And my first cut at it won’t descend into sub-objects.
>
>
> The you wouldn't be doing it right. The whole thing about a recursive descent
> parser is that it's, well, recursive.
Right, but it would serve my immediate needs. I have a column that just stores
key/value pairs with no nesting. So I know I can write something like this and
have it be good enough:
create or replace function json_smash(
json
) RETURNS JSON language SQL STRICT IMMUTABLE AS $$
SELECT format('{%s}', array_to_string(ARRAY(
SELECT format('%s: %s', to_json(key), value)
FROM (
SELECT key, value, row_number() OVER (
partition by key order by rnum desc
) AS rnum
FROM (
SELECT key, value, row_number() OVER (
partition by key
) AS rnum
FROM json_each($1)
) a
) b
WHERE rnum = 1
), ','))::json;
$$;
And do you really want to see that unloosed on the world? :-P (Yes, I know
there is no guarantee on the order of rows returned by json_each()).
Best,
David
--
Sent via pgsql-hackers mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers