Check if key exists in a JSON with PL/pgSQL?

前端 未结 4 1493
时光说笑
时光说笑 2021-01-12 22:18

I\'m trying to check if a key exists in a JSON sent as parameter in a PL/pgSQL function.

Here is the function.

CREATE FUNCTION sp_update_user(user_in         


        
4条回答
  •  借酒劲吻你
    2021-01-12 22:44

    The "json_object_keys" function can be a solution.

    You can select the result of a query that uses this function into a declared variable and then check if your variable is null. Here is an example:

    DECLARE
    test character varying;
    BEGIN
        SELECT a INTO test FROM json_object_keys(user_info) a WHERE a = 'lastname';
        IF (test IS NOT NULL) THEN
            --SUCESSS
        ELSE
            --FAIL
        END IF;
    END;
    

提交回复
热议问题