pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #15242: JSON functions not recognizing JSON
3+ messages / 3 participants
[nested] [flat]
* BUG #15242: JSON functions not recognizing JSON
@ 2018-06-14 17:50 PG Bug reporting form <noreply@postgresql.org>
0 siblings, 2 replies; 3+ messages in thread
From: PG Bug reporting form @ 2018-06-14 17:50 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: david@daily-harvest.com
The following bug has been logged on the website:
Bug reference: 15242
Logged by: David
Email address: david@daily-harvest.com
PostgreSQL version: 9.6.6
Operating system: Mac / DBeaver
Description:
I get this error: SQL Error [22023]: ERROR: cannot deconstruct an array as
an object
when running the statement below because the JSON_EACH function doesn't
recognize the JSON. If I remove the json_each function the query runs fine
and executes JSON_ARRAY_LENGTH on the same variable, even though both
functions take json.
WITH sample_text AS
(
SELECT TEXT '[{"id":11,"name":"Chocolate +
Blueberry","sku":"11-CHOBLU"},{"id":11,"name":"Chocolate +
Blueberry","sku":"11-CHOBLU"}]' AS txt
)
SELECT json_array_length(txt::json), json_each(txt::json) FROM sample_text
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #15242: JSON functions not recognizing JSON
@ 2018-06-14 18:12 David G. Johnston <david.g.johnston@gmail.com>
parent: PG Bug reporting form <noreply@postgresql.org>
1 sibling, 0 replies; 3+ messages in thread
From: David G. Johnston @ 2018-06-14 18:12 UTC (permalink / raw)
To: david@daily-harvest.com <david@daily-harvest.com>; pgsql-bugs@lists.postgresql.org <pgsql-bugs@lists.postgresql.org>
On Thursday, June 14, 2018, PG Bug reporting form <noreply@postgresql.org>
wrote:
> The following bug has been logged on the website:
>
> Bug reference: 15242
> Logged by: David
> Email address: david@daily-harvest.com
> PostgreSQL version: 9.6.6
> Operating system: Mac / DBeaver
> Description:
>
> I get this error: SQL Error [22023]: ERROR: cannot deconstruct an array
> as
> an object
>
> when running the statement below because the JSON_EACH function doesn't
> recognize the JSON. If I remove the json_each function the query runs fine
> and executes JSON_ARRAY_LENGTH on the same variable, even though both
> functions take json.
>
> WITH sample_text AS
> (
> SELECT TEXT '[{"id":11,"name":"Chocolate +
> Blueberry","sku":"11-CHOBLU"},{"id":11,"name":"Chocolate +
> Blueberry","sku":"11-CHOBLU"}]' AS txt
> )
> SELECT json_array_length(txt::json), json_each(txt::json) FROM sample_text
>
This is not a bug, nor is the behavior wrong.
json_each requires that the json being passed to it is a json object, not a
json array. The docs and the error message say as much.
As json_each outputs two columns, one with the key and the other with the
value, and since arrays don't have keys, it follows that json_each would be
unable to operate on them.
David J.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: BUG #15242: JSON functions not recognizing JSON
@ 2018-06-15 00:20 Andrew Gierth <andrew@tao11.riddles.org.uk>
parent: PG Bug reporting form <noreply@postgresql.org>
1 sibling, 0 replies; 3+ messages in thread
From: Andrew Gierth @ 2018-06-15 00:20 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; david@daily-harvest.com
>>>>> "PG" == PG Bug reporting form <noreply@postgresql.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)
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2018-06-15 00:20 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2018-06-14 17:50 BUG #15242: JSON functions not recognizing JSON PG Bug reporting form <noreply@postgresql.org>
2018-06-14 18:12 ` David G. Johnston <david.g.johnston@gmail.com>
2018-06-15 00:20 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox