Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W6YRb-0005pW-PG for pgsql-bugs@arkaria.postgresql.org; Fri, 24 Jan 2014 04:32:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W6YRa-0002nI-Q4 for pgsql-bugs@arkaria.postgresql.org; Fri, 24 Jan 2014 04:32:38 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W6YRZ-0002nB-Q3 for pgsql-bugs@postgresql.org; Fri, 24 Jan 2014 04:32:37 +0000 Received: from relay4-d.mail.gandi.net ([2001:4b98:c:538::196]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W6YRS-0002Gh-W8 for pgsql-bugs@postgresql.org; Fri, 24 Jan 2014 04:32:37 +0000 Received: from mfilter18-d.gandi.net (mfilter18-d.gandi.net [217.70.178.146]) by relay4-d.mail.gandi.net (Postfix) with ESMTP id 65C711720BA; Fri, 24 Jan 2014 05:32:30 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mfilter18-d.gandi.net Received: from relay4-d.mail.gandi.net ([217.70.183.196]) by mfilter18-d.gandi.net (mfilter18-d.gandi.net [10.0.15.180]) (amavisd-new, port 10024) with ESMTP id nY2x5brzM+L6; Fri, 24 Jan 2014 05:32:28 +0100 (CET) X-Originating-IP: 98.27.58.255 Received: from [192.168.10.146] (cpe-098-027-058-255.nc.res.rr.com [98.27.58.255]) (Authenticated sender: adsend@dunslane.net) by relay4-d.mail.gandi.net (Postfix) with ESMTPSA id 7C6761721B3; Fri, 24 Jan 2014 05:32:27 +0100 (CET) Message-ID: <52E1ECD9.6070501@dunslane.net> Date: Thu, 23 Jan 2014 23:32:25 -0500 From: Andrew Dunstan User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130110 Thunderbird/17.0.2 MIME-Version: 1.0 To: Bruce Momjian CC: Tom Lane , daniel.zlatev@gmail.com, pgsql-bugs@postgresql.org Subject: Re: BUG #8218: Error when querying an JSON data, 9.3beta References: <24467.1370798666@sss.pgh.pa.us> <20140124030535.GB8993@momjian.us> In-Reply-To: <20140124030535.GB8993@momjian.us> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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