From: Andrew Dunstan <andrew@dunslane.net>
To: Bruce Momjian <bruce@momjian.us>
Cc: Tom Lane <tgl@sss.pgh.pa.us>
Cc: daniel.zlatev@gmail.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #8218: Error when querying an JSON data, 9.3beta
Date: Thu, 23 Jan 2014 23:32:25 -0500
Message-ID: <52E1ECD9.6070501@dunslane.net> (raw)
In-Reply-To: <20140124030535.GB8993@momjian.us>
References: <E1UlhI8-0000bi-Ln@wrigleys.postgresql.org>
<24467.1370798666@sss.pgh.pa.us>
<20140124030535.GB8993@momjian.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
On 01/23/2014 10:05 PM, Bruce Momjian wrote:
> On Sun, Jun 9, 2013 at 01:24:26PM -0400, Tom Lane wrote:
>> daniel.zlatev@gmail.com writes:
>>> CREATE TABLE products (
>>> data JSON
>>> );
>>> INSERT INTO products(data) VALUES('{"id": 1, "name": "shoes", "in_stock": 5}');
>>> INSERT INTO products(data) VALUES('[1,2,3,4,5]');
>>> SELECT * FROM products WHERE (data->>'in_stock')::integer > 0
>>> Output was:
>>> [Err] ERROR: cannot extract field from a non-object
>>> I can understand the reason behind this error(JSON array don't has fields),
>>> but for me it is very logical postgres to exclude this row from the
>>> returning set, rather to throw an error.
>> Hm. In principle we could allow ->> to return NULL rather than failing
>> when there's no such field, but I'm not sure that would represent good
>> language design. However, this example definitely shows there are some
>> holes in the current set of JSON manipulation functions. The only way
>> to avoid a failure here would be to write something like
>>
>> WHERE (CASE WHEN json_has_field(data, 'in_stock') THEN
>> (data->>'in_stock')::integer ELSE NULL::integer END) > 0
>>
>> but there is no "json_has_field" test function, nor any nice way to
>> build one from the provided functions.
>>
>> It's probably too late to address this for 9.3, but we ought to put it
>> on the to-do list for 9.4.
> Was this addressed for 9.4 because I don't see it?
>
9.4 does have json_typeof(), which should let you construct an adequate
test along the lines Tom suggests.
cheers
andrew
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: andrew@dunslane.net, bruce@momjian.us, tgl@sss.pgh.pa.us, daniel.zlatev@gmail.com
Subject: Re: BUG #8218: Error when querying an JSON data, 9.3beta
In-Reply-To: <52E1ECD9.6070501@dunslane.net>
References: <E1UlhI8-0000bi-Ln@wrigleys.postgresql.org> <24467.1370798666@sss.pgh.pa.us> <20140124030535.GB8993@momjian.us> <52E1ECD9.6070501@dunslane.net>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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