Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W6X5X-0002Bq-P2 for pgsql-bugs@arkaria.postgresql.org; Fri, 24 Jan 2014 03:05:47 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W6X5X-00089L-7t for pgsql-bugs@arkaria.postgresql.org; Fri, 24 Jan 2014 03:05:47 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W6X5V-00089B-8K for pgsql-bugs@postgresql.org; Fri, 24 Jan 2014 03:05:45 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W6X5O-0000kh-9f for pgsql-bugs@postgresql.org; Fri, 24 Jan 2014 03:05:44 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1W6X5L-0006vg-7W; Thu, 23 Jan 2014 22:05:35 -0500 Date: Thu, 23 Jan 2014 22:05:35 -0500 From: Bruce Momjian To: Tom Lane Cc: daniel.zlatev@gmail.com, pgsql-bugs@postgresql.org, Andrew Dunstan Subject: Re: BUG #8218: Error when querying an JSON data, 9.3beta Message-ID: <20140124030535.GB8993@momjian.us> References: <24467.1370798666@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <24467.1370798666@sss.pgh.pa.us> User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -2.5 (--) 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 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? -- Bruce Momjian http://momjian.us EnterpriseDB http://enterprisedb.com + Everyone has their own god. + -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs