Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iLMJr-0006UH-LV for pgsql-general@arkaria.postgresql.org; Fri, 18 Oct 2019 07:05:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iLMJq-0003RW-Gs for pgsql-general@arkaria.postgresql.org; Fri, 18 Oct 2019 07:05:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iLMJq-0003RL-2c for pgsql-general@lists.postgresql.org; Fri, 18 Oct 2019 07:05:02 +0000 Received: from mout.kundenserver.de ([212.227.126.135]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iLMJn-0000Q2-1C for pgsql-general@lists.postgresql.org; Fri, 18 Oct 2019 07:05:00 +0000 Received: from [10.8.0.75] ([185.40.248.10]) by mrelayeu.kundenserver.de (mreue011 [212.227.15.129]) with ESMTPSA (Nemesis) id 1Mq33i-1hiR0E0x3C-00n6v7 for ; Fri, 18 Oct 2019 09:04:56 +0200 Subject: Re: A little confusion about JSON Path To: pgsql-general@lists.postgresql.org References: <8dcb9b9d-4863-b7e9-c3dc-e4ab2d01e918@gmx.net> <1df6657c0e93e58791273e6389a2500b15efd0cd.camel@cybertec.at> From: Thomas Kellerer Message-ID: Date: Fri, 18 Oct 2019 09:04:54 +0200 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 MIME-Version: 1.0 In-Reply-To: <1df6657c0e93e58791273e6389a2500b15efd0cd.camel@cybertec.at> Content-Type: text/plain; charset=utf-8 Content-Language: de-DE Content-Transfer-Encoding: 7bit X-Provags-ID: V03:K1:SGrExFgCS9gqL2vaYxVVJsuCoKqTNG3wEr6BWE+RAhSyqpx3rd0 FfNzjnvFZRECUSts4vrpJC8WR+D4CFVu0uln8rcR9cMUj2+1oqrzne6FwZTQdOBNY5pHrv+ HlYvbY4Xw9Yhvq4sd7TxSf2eAUyE1O/fvmFeYIW0ami/HcbgJWlrdh+QOSGpi+Ehi1NbOx2 CWSRAwB+p7vZ23sfEEiOw== X-Spam-Flag: NO X-UI-Out-Filterresults: notjunk:1;V03:K0:AAuPgrYJBBg=:/R7VZ9ce5akEB+zEX7Ca3o gEfPRB+FIgabwOBm+30mircvTMzNuJoBRO+aqFoFkRDkpmQ3DavPtRxu9e4wpRv8tfsrXanqp lYuFUehzuAqGNmQHkWNaNa0Mf61/QEC8d/g5PcSTqj5m9/HNdgBxEylHRUU7tpKio17Sj30vB zyF1+1+NIL0Gthn8uyA/5zLNS2EPxWUGTtO67NGfAqwPMAb/J/jRG7H5bhncHsKmc9AnqdC/F aBkLAoVrtzSdveq1Cbv5UdOre8L778tPZMjRbo453El7Mo5WDO3AY6otCchDVLTVJyhROCcGg Y6dMrZ5j40lc2AZTk3IWD4AjBvKb3fwOOHbLvNe0EDdjrAVjtBM117gLQToDvpIKBwElmZBqb m7zQmbcGKuPRsVlI9ch+8BqTQV0OzXyt5y7XSJzU5OoNYG8F6wo3cOfTfBnu2+iVUOtEgRUbr BcjKmwkgE2sDqxN+jypzY4q9WHb0bTuwWbJalShhlUX6U8Y4M4HoiycyQKf3Oy4d15ZijkN+T iIO6b4Ci/ntoDb8pjcLPzHoQfYoLkzcDmtKWqUaKKgTIRoWcDPs36tm040MPWq7S6f3xLC8h1 6d5C92VL5e649LWdq4sTcXzywQWserNE5eFeoaTTeiA/1gM75lBFf2P+PIbI36vIawMfQIEia G/xZwNeuAgXA0UdDQoEp9xGeRHMG78kWt0r7sFDAVDgH5CrLXRYBt3xz/RQVuAluprBe9vORH liYyitl/kvb3OgXaAOWy5UQVzd4Lo5xRRsdHM2ag+UWS0YoyGA+FdhbBmErmt9rSfT7bHxcNJ EfBb514ji0Y+1cYaqo9xJ5qtr8/GFRoIQuz4Q5kSaZIQ76aRmGTrmxSWK3KzrSO3qVDqHt7Z0 zwBkvGym23eTln1fivtWkZTbeNmNwpHNfpiykXTh4= List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Laurenz Albe schrieb am 17.10.2019 um 13:25: >> I don't understand why the following two JSON Path expressions aren't doing the same thing in Postgres 12: >> >> jsonb_path_exists(data, '$.k1.list.type() ? (@ == "array")'), -- returns true as expected >> jsonb_path_exists(data, '$.k1.list ? (@.type() == "array")') -- returns false - not expected >> >> Apparently "@.type()" returns something different then "$.k1.list.type()" >> > This seems to be a consequence of "lax" mode: > > "Besides, comparison operators automatically unwrap their operands in the lax mode, > so you can compare SQL/JSON arrays out-of-the-box. An array of size 1 is considered > equal to its sole element. Automatic unwrapping is not performed only when: > > - The path expression contains type() or size() methods that return the type and > the number of elements in the array, respectively. > > (from https://www.postgresql.org/docs/12/functions-json.html) > > with sample (data) as ( > values > ('{"k1": {"list":[1,2,3]}}'::jsonb) > ) > select data, > jsonb_path_exists(data, '$.k1.list ? (@.type() == "number")'), -- lax mode unwraps the array > jsonb_path_exists(data, 'strict $.k1.list ? (@.type() == "array")') -- strict mode doesn't > from sample; Ah, thanks. I did not see that part.