Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fTcTN-0008Ct-Kr for pgsql-bugs@arkaria.postgresql.org; Fri, 15 Jun 2018 00:20:13 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fTcTL-0006lx-E5 for pgsql-bugs@arkaria.postgresql.org; Fri, 15 Jun 2018 00:20:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fTcTL-0006lo-7f for pgsql-bugs@lists.postgresql.org; Fri, 15 Jun 2018 00:20:11 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fTcTH-0004Yb-Ez for pgsql-bugs@lists.postgresql.org; Fri, 15 Jun 2018 00:20:10 +0000 Received: from [192.168.127.1] (port=27936 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1fTcTD-000PM9-Ni; Fri, 15 Jun 2018 00:20:03 +0000 Received: from [127.0.0.1] (port=18802 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1fTcTC-000BJv-So; Fri, 15 Jun 2018 00:20:02 +0000 From: Andrew Gierth To: pgsql-bugs@lists.postgresql.org, david@daily-harvest.com Subject: Re: BUG #15242: JSON functions not recognizing JSON In-Reply-To: <152899864499.24805.18088125700685635313@wrigleys.postgresql.org> (PG Bug reporting form's message of "Thu, 14 Jun 2018 17:50:44 +0000") Message-ID: <87a7rwsy9z.fsf@news-spur.riddles.org.uk> References: <152899864499.24805.18088125700685635313@wrigleys.postgresql.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Fri, 15 Jun 2018 01:20:02 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "PG" == PG Bug reporting form 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)