Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n59h9-00037q-4p for pgsql-hackers@arkaria.postgresql.org; Wed, 05 Jan 2022 17:03:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n59h7-0000Lm-Lt for pgsql-hackers@arkaria.postgresql.org; Wed, 05 Jan 2022 17:03:25 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n59h7-0000Ld-CU for pgsql-hackers@lists.postgresql.org; Wed, 05 Jan 2022 17:03:25 +0000 Received: from relay2-d.mail.gandi.net ([217.70.183.194]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n59h4-0002HG-5v for pgsql-hackers@postgresql.org; Wed, 05 Jan 2022 17:03:24 +0000 Received: (Authenticated sender: adsend@dunslane.net) by relay2-d.mail.gandi.net (Postfix) with ESMTPSA id 59CF54000A; Wed, 5 Jan 2022 17:03:17 +0000 (UTC) Message-ID: <530d18d8-e4d8-f0e6-c7ff-e499672710c0@dunslane.net> Date: Wed, 5 Jan 2022 12:03:15 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.2.0 Subject: Re: SQL/JSON: functions Content-Language: en-US To: Himanshu Upadhyaya Cc: Nikita Glukhov , PostgreSQL-development , Dmitry Dolgov <9erthalion6@gmail.com>, Oleg Bartunov , Erik Rijkers References: <5f8987e9-4c3d-6402-b18c-b3e84b786739@postgrespro.ru> <0feb891c5a60d17eca49dc77c93d86fb@xs4all.nl> <24c35f9a-d11d-855e-ebfc-5638993c8d99@postgrespro.ru> <55a4a276-2ed7-077b-8c4e-8209b58ebbf5@dunslane.net> <264268a3-f836-487f-b56e-0a19739ca03b@dunslane.net> <97da94b2-dcfd-20cf-2ed7-b9f68c6b6c16@2ndQuadrant.com> <50df2280-7c7a-dffd-a942-9c67b1550e2c@dunslane.net> From: Andrew Dunstan In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 1/5/22 00:51, Himanshu Upadhyaya wrote: > On Thu, Dec 9, 2021 at 7:34 PM Himanshu Upadhyaya > wrote: >> 3) >> Is not that result of the two below queries should match because both are trying to retrieve the information from the JSON object. >> >> postgres=# SELECT JSON_OBJECT('track' VALUE '{ >> "segments": [ >> { >> "location": [ 47.763, 13.4034 ], >> "start time": "2018-10-14 10:05:14", >> "HR": 73 >> }, >> { >> "location": [ 47.706, 13.2635 ], >> "start time": "2018-10-14 101:39:21", >> "HR": 135 >> } >> ] >> } >> }')->'track'->'segments'; >> ?column? >> ---------- >> >> (1 row) >> >> postgres=# select '{ >> "track": { >> "segments": [ >> { >> "location": [ 47.763, 13.4034 ], >> "start time": "2018-10-14 10:05:14", >> "HR": 73 >> }, >> { >> "location": [ 47.706, 13.2635 ], >> "start time": "2018-10-14 10:39:21", >> "HR": 135 >> } >> ] >> } >> }'::jsonb->'track'->'segments'; >> ?column? >> ------------------------------------------------------------------------------------------------------------------------------------------------------------------- >> [{"HR": 73, "location": [47.763, 13.4034], "start time": "2018-10-14 10:05:14"}, {"HR": 135, "location": [47.706, 13.2635], "start time": "2018-10-14 10:39:21"}] >> (1 row) >> > just wanted to check your opinion on the above, is this an expected behaviour? Your VALUE clause is actually not legal JSON - it has one too many braces at the end. The reason postgres didn't complain about it is that JSON_OBJECT is treating it as a string. If you correct the JSON and cast it as jsonb you get the desired result: andrew=# SELECT JSON_OBJECT('track' VALUE '{ "segments": [ { "location": [ 47.763, 13.4034 ], "start time": "2018-10-14 10:05:14", "HR": 73 }, { "location": [ 47.706, 13.2635 ], "start time": "2018-10-14 101:39:21", "HR": 135 } ] }'::jsonb)->'track'->'segments'; ?column? -------------------------------------------------------------------------------------------------------------------------------------------------------------------- [{"HR": 73, "location": [47.763, 13.4034], "start time": "2018-10-14 10:05:14"}, {"HR": 135, "location": [47.706, 13.2635], "start time": "2018-10-14 101:39:21"}] (1 row) >> Few comments For 0002-SQL-JSON-constructors-v59.patch: > Also, any thoughts on this? I will look at that separately. cheers andrew -- Andrew Dunstan EDB: https://www.enterprisedb.com