How can I use a parameter within a JSONPath expression in a PostgreSQL SQL query?
I want to query something like this:
SELECT * FROM table WHERE jsondata @@ '$.pathto.array[*].** ? (@.someproperty == ":VALUE").anotherproperty != null';
I tried:
SELECT * FROM table WHERE jsondata @@ '$.pathto.array[*].** ? (@.someproperty == "1").anotherproperty != null';
This query worked, but I can’t use it with a bind parameter ("1").
2
Answers
Use
jsonb_path_match(target jsonb, path jsonpath, vars jsonb)
and pass your variables in the third parameter,vars
, as ajsonb
literal or injsonb_build_object()
: demoTry using this approach , it will let you use the parameter within jsonpath expression.
Hope it works 🙂