As stated in the manual :
jsonb_path_query_first returns the first JSON item returned by the
JSON path for the specified JSON value
OK, but how is the returned list of json items ordered by default before selecting the first json item of this list ?
Is there any way to order this list so that to control the first json item returned by jsonb_path_query_first
, like an ORDER BY
clause in a SELECT DISTINCT
sql statement ?
In the following example, I would like to select the first json object whose value matches with the regular expression ‘xxx’ according to the order ORDERED BY key :: integer ASC
, ie getting the ‘aaxxxaa’ value as result, whereas the returned value by default is ‘ddxxxee’ :
select jsonb_path_query_first('{"-3":"aaxxxaa", "1":"bbcc", "2":"ddxxxee"}' :: jsonb, '$.keyvalue() ? (@.value like_regex "xxx").value')