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

后端 未结 2 901
一整个雨季
一整个雨季 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;
    
    0 讨论(0)
  • 2020-12-23 12:10

    To add the value use the JSON array append opperator (||)

    UPDATE jsontesting
    SET jsondata = jsondata || '["newString"]'::jsonb
    WHERE id = 7;
    

    Removing the value looks like this

    UPDATE jsontesting
    SET jsondata = jsondata - "newString"
    WHERE id = 7; 
    

    Concatenating to a nested field looks like this

    UPDATE jsontesting
    SET jsondata = jsonb_set(
      jsondata::jsonb,
      array['nestedfield'],
      (jsondata->'nestedfield')::jsonb || '["newString"]'::jsonb) 
    WHERE id = 7;
    
    0 讨论(0)
提交回复
热议问题