Assuming you are building an SQL command to dynamically execute with EXECUTE
in a Postgres function.
To check whether any top-level key has the given value, the most elegant way will be an SQL/JSON path expression. This can be supported with an index and should be decently fast. No need to expensively extract all values into an array first.
Basic example to check for the value 2
:
SELECT jsonb_path_exists(jsonb '{"a":1, "b":2}', '$.* ? (@ == 2)');
To pass in your variable usr_id_
of unknown type (in place of the numeric value 2
of the basic example):
SELECT jsonb_path_exists(jsonb '{"a":1, "b":2}'
, '$.* ? (@ == $usr_id)'
, jsonb_build_object('usr_id', 1));
Concatenating the WHERE
clause in your PL/pgSQL code block can look like this:
IF json_array_length(tms_hlpr_usr_has_authority_fr_srvc_requests(usr_id_,org_id_)) > 0 THEN
_extra_where = _extra_where
|| $j$ AND jsonb_path_exists(srvc_req.form_data, '$.* ? (@ == $usr_id)', $1) AND srvc_req.is_deleted IS NOT TRUE $j$;
END IF;
Using dollar-quoting to allow for nested single quotes. See:
Supply the value for the parameter symbol $1
in a USING
clause to the EXECUTE
command. Like:
EXECUTE 'SELECT * FROM srvc_req ' || _extra_where
USING jsonb_build_object('usr_id', usr_id_); -- $1 !
Related:
(This would be much easier to demonstrate if you had provided your actual minimal function.)