Home > database >  postgresql UPDATE to remove key/value from JSONB data type
postgresql UPDATE to remove key/value from JSONB data type

Time:11-03

Given the following table and data:

CREATE TABLE test (  
  slots jsonb
);

INSERT INTO test VALUES ('{"0": {"tag": "abc", "info": "xyz"}, "1": {"tag": "def", "info": "uvw"}}');
SELECT slots FROM test;

Now I want to delete the "1" key/value entirely. I can set it to null as follows:

UPDATE test SET slots['1'] = null;
SELECT slots FROM test;

But this returns:

{"0": {"tag": "abc", "info": "xyz"}, "1": null}

Whereas I want it to return:

{"0": {"tag": "abc", "info": "xyz"}}

What is the command/syntax to achieve this?

CodePudding user response:

Use the - operator to remove a key completely

update test 
  set slots = slots - '1'
  • Related