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

前端 未结 4 1532
时光说笑
时光说笑 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:34

    Found a simpler solution that checks if the json key have a value.

    IF (user_info->>'username') IS NOT NULL
    THEN
          RAISE NOTICE 'username';
          UPDATE users SET username = user_info->>'username' WHERE id = sp_update_user.user_id;
    END IF;
    

    This checks if the key 'username' in the json have a volume other than NULL.

提交回复
热议问题