Re: BUG #15242: JSON functions not recognizing JSON

From: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org, david(at)daily-harvest(dot)com
Subject: Re: BUG #15242: JSON functions not recognizing JSON
Date: 2018-06-15 00:20:02
Message-ID: 87a7rwsy9z.fsf@news-spur.riddles.org.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

>>>>> "PG" == PG Bug reporting form <noreply(at)postgresql(dot)org> writes:

PG> I get this error: SQL Error [22023]: ERROR: cannot deconstruct an
PG> array as an object

PG> when running the statement below because the JSON_EACH function
PG> doesn't recognize the JSON.

It recognizes the json just fine, and so it can tell that it's an array
rather than the expected object. You were looking for
json_array_elements rather than (or prior to) json_each if you want to
enumerate the objects:

-- to get one row per object:

select json_array_length(txt::json), a.value
from sample_text, json_array_elements(txt::json) as a;

-- to get one row per object key:

select json_array_length(txt::json), a.value, a.ord, e.key, e.value
from sample_text,
json_array_elements(txt::json) with ordinality as a(value,ord),
json_each(a.value) as e;

--
Andrew (irc:RhodiumToad)

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message Justin Pryzby 2018-06-15 01:38:00 Re: BUG #15237: I got "ERROR: source for a multiple-column UPDATE item must be a sub-SELECT or ROW() expression"
Previous Message Tom Lane 2018-06-14 23:28:24 Re: row_to_json(), NULL values, and AS