Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1abHi7-0006F9-29 for pgsql-bugs@arkaria.postgresql.org; Thu, 03 Mar 2016 01:05:47 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1abHi6-0004XI-L7 for pgsql-bugs@arkaria.postgresql.org; Thu, 03 Mar 2016 01:05:46 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1abHhS-00032E-OF for pgsql-bugs@postgresql.org; Thu, 03 Mar 2016 01:05:06 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1abHhO-0005Yu-Ll for pgsql-bugs@postgresql.org; Thu, 03 Mar 2016 01:05:05 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id u2314xOm026374; Wed, 2 Mar 2016 20:05:00 -0500 From: Tom Lane To: johann@visagie.za.net cc: pgsql-bugs@postgresql.org, Andrew Dunstan Subject: Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances In-reply-to: <20160302080310.2629.98690@wrigleys.postgresql.org> References: <20160302080310.2629.98690@wrigleys.postgresql.org> Comments: In-reply-to johann@visagie.za.net message dated "Wed, 02 Mar 2016 08:03:10 +0000" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <26312.1456967008.0@sss.pgh.pa.us> Date: Wed, 02 Mar 2016 20:04:59 -0500 Message-ID: <26373.1456967099@sss.pgh.pa.us> 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 ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="US-ASCII" Content-ID: <26312.1456967008.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable johann@visagie.za.net writes: > SELECT t.* > FROM json_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8}'::json) > AS t(a int, b json, c text, x int); > However, if (as in this example) another key in the JSON object - `b` in > this case - refers to a nested JSON object which *does* contain a key `c`, > the function returns a malformed JSON string as (text) value for column > `c`: > a | b | c | x > ---+----------------+---------+--- > 1 | {"c":16,"d":2} | {"c":16 | 8 > (1 row) AFAICT this is a simple thinko in the hash_object_field_end() callback, as per attached patch that fixes this and doesn't break any existing regression test cases. Andrew, do you concur that this is correct, or is there something I'm missing about the tracking of lex_level? regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-nested-objects-in-json-to-record.patch"; charset="us-ascii" Content-ID: <26312.1456967008.2@sss.pgh.pa.us> Content-Description: fix-nested-objects-in-json-to-record.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/json= funcs.c index 88225aa..363afa7 100644 *** a/src/backend/utils/adt/jsonfuncs.c --- b/src/backend/utils/adt/jsonfuncs.c *************** hash_object_field_end(void *state, char=20 *** 2438,2444 **** /* * Ignore nested fields. */ ! if (_state->lex->lex_level > 2) return; =20=20 /* --- 2438,2444 ---- /* * Ignore nested fields. */ ! if (_state->lex->lex_level > 1) return; =20=20 /* ------- =_aaaaaaaaaa0 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs ------- =_aaaaaaaaaa0--