pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedA little confusion about JSON Path
3+ messages / 2 participants
[nested] [flat]
* A little confusion about JSON Path
@ 2019-10-16 12:03 Thomas Kellerer <spam_eater@gmx.net>
0 siblings, 1 reply; 3+ messages in thread
From: Thomas Kellerer @ 2019-10-16 12:03 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hello,
I don't understand why the following two JSON Path expressions aren't doing the same thing in Postgres 12:
with sample (data) as (
values
('{"k1": {"list":[1,2,3]}}'::jsonb)
)
select data,
jsonb_path_exists(data, '$.k1.list.type() ? (@ == "array")'), -- returns true as expected
jsonb_path_exists(data, '$.k1.list ? (@.type() == "array")') -- returns false - not expected
from sample;
Apparently "@.type()" returns something different then "$.k1.list.type()"
But maybe I simply don't understand how the @ is supposed to work.
Regards
Thomas
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: A little confusion about JSON Path
@ 2019-10-17 11:25 Laurenz Albe <laurenz.albe@cybertec.at>
parent: Thomas Kellerer <spam_eater@gmx.net>
0 siblings, 1 reply; 3+ messages in thread
From: Laurenz Albe @ 2019-10-17 11:25 UTC (permalink / raw)
To: Thomas Kellerer <spam_eater@gmx.net>; pgsql-general@lists.postgresql.org
Thomas Kellerer wrote:
> I don't understand why the following two JSON Path expressions aren't doing the same thing in Postgres 12:
>
> with sample (data) as (
> values
> ('{"k1": {"list":[1,2,3]}}'::jsonb)
> )
> select data,
> jsonb_path_exists(data, '$.k1.list.type() ? (@ == "array")'), -- returns true as expected
> jsonb_path_exists(data, '$.k1.list ? (@.type() == "array")') -- returns false - not expected
> from sample;
>
>
> Apparently "@.type()" returns something different then "$.k1.list.type()"
>
> But maybe I simply don't understand how the @ is supposed to work.
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;
data | jsonb_path_exists | jsonb_path_exists
-----------------------------+-------------------+-------------------
{"k1": {"list": [1, 2, 3]}} | t | t
(1 row)
Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.com
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: A little confusion about JSON Path
@ 2019-10-18 07:04 Thomas Kellerer <spam_eater@gmx.net>
parent: Laurenz Albe <laurenz.albe@cybertec.at>
0 siblings, 0 replies; 3+ messages in thread
From: Thomas Kellerer @ 2019-10-18 07:04 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
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.
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2019-10-18 07:04 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-10-16 12:03 A little confusion about JSON Path Thomas Kellerer <spam_eater@gmx.net>
2019-10-17 11:25 ` Laurenz Albe <laurenz.albe@cybertec.at>
2019-10-18 07:04 ` Thomas Kellerer <spam_eater@gmx.net>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox