Appending (pushing) and removing from a JSON array in PostgreSQL 9.5+

后端 未结 2 913
一整个雨季
一整个雨季 2020-12-23 11:20

For versions less than 9.5 see this question

I have created a table in PostgreSQL using this:

CREATE TEMP TABLE jsontesting
AS
  SELECT id, jsondat         


        
2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-23 11:56

    To add to Evan Carroll's answer, you may want to do the following to set the column to an empty array if it is NULL. The append operator (||) does nothing if the column is currently NULL.

    UPDATE jsontesting SET jsondata = (
        CASE
            WHEN jsondata IS NULL THEN '[]'::JSONB
            ELSE jsondata
        END
    ) || '["newString"]'::JSONB WHERE id = 7;
    

提交回复
热议问题