I am running postgresql 12 and am trying to use jsonpath.
I'd like to use jsonb_path_query_first to get the length of an array. I know I can do json_array_length but would rather not.
Here is the query that I am trying to make work.
select jsonb_path_query_first('{"a":3,"b":6,"s":[1,2,3,4,5], "d":{"v":4}}'::jsonb, '$.s.length()'::jsonpath);
ERROR: syntax error, unexpected '(', expecting end of file at or near "(" of jsonpath input
LINE 1: ...a":3,"b":6,"s":[1,2,3,4,5], "d":{"v":4}}'::jsonb, '$.s.lengt...
Does the version of jsonpath in postgresql 12 not support this type of jsonpath functionality.
CodePudding user response:
You can use the .size()
method for this:
test# select
jsonb_path_query_first(
'{"a":3,"b":6,"s":[1,2,3,4,5], "d":{"v":4}}'::jsonb,
'$.s.size()'::jsonpath
);
jsonb_path_query_first
════════════════════════
5
(1 row)