Re: json_array_elements_text?

From: Marti Raudsepp <marti(at)juffo(dot)org>
To: Laurence Rowe <l(at)lrowe(dot)co(dot)uk>
Cc: PostgreSQL <pgsql-general(at)postgresql(dot)org>
Subject: Re: json_array_elements_text?
Date: 2014-01-20 10:54:29
Message-ID: CABRT9RB4ii8EFNn=XRevzSuM8DC2HYf5pEuvgnm15EPaN8er8g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Fri, Jan 17, 2014 at 10:20 AM, Laurence Rowe <l(at)lrowe(dot)co(dot)uk> wrote:
> I'm trying to unpack a json array into it's constituent text values so I can
> join them to a table. I can successfully unpack json values, but am having
> trouble converting these to text so I can cast them to the UUIDs needed for
> the join.

Here's another approach to implement json_array_elements_text, using
the field extraction operator to extract all fields one-by-one. But
for large arrays this is likely slower, as it needs to parse the whole
JSON string for each array element, leading to O(n^2) complexity.

CREATE FUNCTION json_array_elements_text(json) RETURNS SETOF text
IMMUTABLE LANGUAGE sql
AS $$ SELECT $1->>i FROM generate_series(0, json_array_length($1)-1) i; $$;

db=# select json_array_elements_text('["hello",1.3,"\u2603"]');
json_array_elements_text
--------------------------
hello
1.3

Regards,
Marti

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Boszormenyi Zoltan 2014-01-20 11:06:23 Re: using rpmbuild with PostgreSQL 9.2.6 source code
Previous Message Marti Raudsepp 2014-01-20 10:34:31 Re: json_array_elements_text?