0

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.

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.