1

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?

1 Answer 1

4

Use the - operator to remove a key completely

update test 
  set slots = slots - '1'
Sign up to request clarification or add additional context in comments.

2 Comments

P.S. does this apply to postgresql 14 too? I know it will work, but postgresql 14 comes with new operators for accessing jsonb data, so just checking there isn't a new version of the - operator.
I don't think the new subscripting supports removal of keys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.