PostgreSQL delete a nested key of json column

Viewed 54

I am using postgres DB in my application. Consider below is my json object in postgres table

{
"key1":"abc",
"key2":{
  "key3":"xyz"
}
}

If i execute the below query

 UPDATE table SET data = (data - 'key1');   

key1 got deleted and updated in DB. But i need to delete key3(which is inside key2), i couldn't find any exact query to delete that. Kindly post your ideas.

1 Answers

You can use the #- operator:

deletes the field or array element at the specified path, where path elements can be either field keys or array indexes.

update the_table
   set data = data #- '{key2, key3}'
Related