drmrbrewer
drmrbrewer

Reputation: 12989

postgresql UPDATE to remove key/value from JSONB data type

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?

Upvotes: 1

Views: 2831

Answers (1)

user330315
user330315

Reputation:

Use the - operator to remove a key completely

update test 
  set slots = slots - '1'

Upvotes: 4

Related Questions