Re: Duplicate JSON Object Keys - Mailing list pgsql-hackers

From David E. Wheeler
Subject Re: Duplicate JSON Object Keys
Date
Msg-id 3BA2E36E-70AD-4E1B-962E-5AC91A64E486@justatheory.com
Whole thread Raw
In response to Re: Duplicate JSON Object Keys  (Andrew Dunstan <andrew@dunslane.net>)
List pgsql-hackers
On Mar 13, 2013, at 10:59 AM, Andrew Dunstan <andrew@dunslane.net> 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
knowI 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
returnedby json_each()). 

Best,

David


pgsql-hackers by date:

Previous
From: Andrew Dunstan
Date:
Subject: Re: Duplicate JSON Object Keys
Next
From: Tom Lane
Date:
Subject: Re: Column defaults for foreign tables (was Re: [v9.3] writable foreign tables)