Postgresql - update or remove value from nested jsonb element

Is there a way to remove / update the nested json key (not array) object like the following json:

{
  "top": {
    "nested": {
       "leaf": 1
    }
  }  
}

      

how can i delete / update a sheet item?

I tried

SELECT jsonb '{"top": {"nested": {"leaf" : 1}}' - '{top,nested,leaf}'

but no luck

+3


source to share


1 answer


You need to use the operator #-

, not -

:

SELECT jsonb '{"top": {"nested": {"leaf" : 1}}}' #- '{top,nested,leaf}';
┌─────────────────────────┐
│        ?column?         │
├─────────────────────────┤
│ {"top": {"nested": {}}} │
└─────────────────────────┘
(1 row)

      




In the documentation:

  • -

    (with argument text

    ): Remove key / value pair or string element from left operand. A key / value pair is matched based on their key value.
  • -

    (with argument int

    ): remove the array element at the specified index (the number of negative integers from the end). Throws an error if the top-level container is not an array.
  • #-

    : remove field or element with specified path (for JSON arrays, negative integers from the end)
+8


source







All Articles