Thread: Delete values from JSON

Delete values from JSON

From
Andreas Joseph Krogh
Date:

Hi, in PG-14, how do I delete the keys "dunsNumber": "NaN":

 

{  "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6",  "details": [    {      "keyInformation": {        "dunsNumber": "NaN",        "organizationType": "LIMITED_COMPANY"      }    },    {      "keyInformation": {        "dunsNumber": "123",        "organizationType": "LIMITED_COMPANY"      }    }  ],  "nisse": 123
}

 

So that the result becomes:

 

{  "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6",  "details": [    {      "keyInformation": {        "organizationType": "LIMITED_COMPANY"      }    },    {      "keyInformation": {        "dunsNumber": "123",        "organizationType": "LIMITED_COMPANY"      }    }  ],  "nisse": 123
}

 

Thanks.

 

--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
Attachment

Re: Delete values from JSON

From
Romain MAZIÈRE
Date:

Hi,

If it is jsonb type, you can have a look at the documentation : https://www.postgresql.org/docs/14/functions-json.html

There are some examples :

jsonb - textjsonb

Deletes a key (and its value) from a JSON object, or matching string value(s) from a JSON array.

'{"a": "b", "c": "d"}'::jsonb - 'a'{"c": "d"}

'["a", "b", "c", "b"]'::jsonb - 'b'["a", "c"]

jsonb - text[]jsonb

Deletes all matching keys or array elements from the left operand.

'{"a": "b", "c": "d"}'::jsonb - '{a,c}'::text[]{}

jsonb - integerjsonb

Deletes the array element with specified index (negative integers count from the end). Throws an error if JSON value is not an array.

'["a", "b"]'::jsonb - 1["a"]

jsonb #- text[]jsonb

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

'["a", {"b":1}]'::jsonb #- '{1,b}'["a", {}]

Regards

Romain MAZIÈRE
romain.maziere@sigmaz-consilium.fr
+33.535.545.085
+33.781.46.36.96
https://sigmaz-consilium.fr
Le 17/03/2023 à 08:56, Andreas Joseph Krogh a écrit :
.ck-content { --ck-color-image-caption-background: #f7f7f7; --ck-color-image-caption-text: #333333; --ck-color-mention-background: #990030E6; --ck-color-mention-text: #990030; --ck-color-table-caption-background: #f7f7f7; --ck-color-table-caption-text: #333333; --ck-highlight-marker-blue: #72ccfd; --ck-highlight-marker-green: #62f962; --ck-highlight-marker-pink: #fc7899; --ck-highlight-marker-yellow: #fdfd77; --ck-highlight-pen-green: #128a00; --ck-highlight-pen-red: #e71313; --ck-image-style-spacing: 1.5em; --ck-spacing-large: 2px; --ck-inline-image-style-spacing: calc(var(--ck-image-style-spacing) / 2); --ck-todo-list-checkmark-size: 16px; /* This works when email is READ in Visena-reader, because there the content of the email is in "shadow-DOM", which has it's own :root, but needs to be declared in ".ck-content p, .ck-content div" as well for the actual CKEditor to display it correctly, else it get's its default from .liftTemplate */ font-family: Arial, Helvetica, sans-serif; font-size: 14px; }.ck-content .image.image_resized { max-width: 100%; display: block; box-sizing: border-box; }.ck-content .image.image_resized img { width: 100%; }.ck-content .image.image_resized > figcaption { display: block; }.ck-content .image > figcaption { display: table-caption; caption-side: bottom; word-break: break-word; color: var(--ck-color-image-caption-text); background-color: var(--ck-color-image-caption-background); padding: .6em; font-size: .75em; outline-offset: -1px; }.ck-content p, .ck-content div { font-family: Arial, Helvetica, sans-serif; font-size: 14px; }.ck-content p.visenaNormal { margin: 0; }.ck-content .text-tiny { font-size: .7em; }.ck-content .text-small { font-size: .85em; }.ck-content .text-big { font-size: 1.4em; }.ck-content .text-huge { font-size: 1.8em; }.ck-content .image-style-block-align-left, .ck-content .image-style-block-align-right { max-width: calc(100% - var(--ck-image-style-spacing)); }.ck-content .image-style-align-left, .ck-content .image-style-align-right { clear: none; }.ck-content .image-style-side { float: right; margin-left: var(--ck-image-style-spacing); max-width: 50%; }.ck-content .image-style-align-left { float: left; margin-right: var(--ck-image-style-spacing); }.ck-content .image-style-align-center { margin-left: auto; margin-right: auto; }.ck-content .image-style-align-right { float: right; margin-left: var(--ck-image-style-spacing); }.ck-content .image-style-block-align-right { margin-right: 0; margin-left: auto; }.ck-content .image-style-block-align-left { margin-left: 0; margin-right: auto; }.ck-content p + .image-style-align-left, .ck-content p + .image-style-align-right, .ck-content p + .image-style-side { margin-top: 0; }.ck-content .image-inline.image-style-align-left, .ck-content .image-inline.image-style-align-right { margin-top: var(--ck-inline-image-style-spacing); margin-bottom: var(--ck-inline-image-style-spacing); }.ck-content .image-inline.image-style-align-left { margin-right: var(--ck-inline-image-style-spacing); }.ck-content .image-inline.image-style-align-right { margin-left: var(--ck-inline-image-style-spacing); }.ck-content .image { display: table; clear: both; text-align: center; margin: 0 auto; min-width: 50px; }.ck-content .image img { display: block; margin: 0 auto; max-width: 100%; min-width: 100%; }.ck-content .image-inline { /* * Normally, the .image-inline would have "display: inline-block" and "img { width: 100% }" (to follow the wrapper while resizing).; * Unfortunately, together with "srcset", it gets automatically stretched up to the width of the editing root. * This strange behavior does not happen with inline-flex. */ display: inline-flex; max-width: 100%; align-items: flex-start; }.ck-content .image-inline picture { display: flex; }.ck-content .image-inline picture, .ck-content .image-inline img { flex-grow: 1; flex-shrink: 1; max-width: 100%; }.ck-content .marker-yellow { background-color: var(--ck-highlight-marker-yellow); }.ck-content .marker-green { background-color: var(--ck-highlight-marker-green); }.ck-content .marker-pink { background-color: var(--ck-highlight-marker-pink); }.ck-content .marker-blue { background-color: var(--ck-highlight-marker-blue); }.ck-content .pen-red { color: var(--ck-highlight-pen-red); background-color: transparent; }.ck-content .pen-green { color: var(--ck-highlight-pen-green); background-color: transparent; }.ck-content hr { margin: 15px 0; height: 4px; background: #dedede; border: 0; }.ck-content blockquote { overflow: hidden; padding-right: 0; padding-left: 1ex; margin-left: 0; margin-right: 0; font-style: unset; border-left: solid 1px #cccccc; }.ck-content .blockquote { font-style: unset; }.ck-content[dir="rtl"] blockquote { border-left: 0; border-right: solid 1px #cccccc; }.ck-content code { background-color: #c7c7c7; padding: 0 1px; font-size: small; border-radius: 2px; }.ck-content .table > figcaption { display: table-caption; caption-side: top; word-break: break-word; text-align: center; color: var(--ck-color-table-caption-text); background-color: var(--ck-color-table-caption-background); padding: .6em; font-size: .75em; outline-offset: -1px; }.ck-content .table { margin: 0 auto; display: table; }.ck-content .table table { border-collapse: collapse; border-spacing: 0; width: 100%; height: 100%; border: 1px double #b3b3b3; }.ck-content .table table td, .ck-content .table table th { min-width: 2em; padding: .4em; border: 1px solid #bfbfbf; }.ck-content .table table th { font-weight: bold; background: #000000E6; }.ck-content[dir="rtl"] .table th { text-align: right; }.ck-content[dir="ltr"] .table th { text-align: left; }.ck-content .table { margin-left: 0; }.ck-content .table table { }.ck-content .table table td { }.ck-content .page-break { position: relative; clear: both; padding: 5px 0; display: flex; align-items: center; justify-content: center; }.ck-content .page-break::after { content: ''; position: absolute; border-bottom: 2px dashed #c4c4c4; width: 100%; }.ck-content .page-break__label { position: relative; z-index: 1; padding: .3em .6em; display: block; text-transform: uppercase; border: 1px solid #c4c4c4; border-radius: 2px; font-family: Arial, Helvetica, sans-serif; font-size: 0.75em; font-weight: bold; color: #333333; background: #ffffff; box-shadow: 2px 2px 1px #000000; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; }.ck-content .media { clear: both; margin: 0 0; display: block; min-width: 15em; }.ck-content .todo-list { list-style: none; }.ck-content .todo-list li { margin-bottom: 5px; }.ck-content .todo-list li .todo-list { margin-top: 5px; }.ck-content .todo-list .todo-list__label > input { -webkit-appearance: none; display: inline-block; position: relative; width: var(--ck-todo-list-checkmark-size); height: var(--ck-todo-list-checkmark-size); vertical-align: middle; border: 0; left: -25px; margin-right: -15px; right: 0; margin-left: 0; }.ck-content .todo-list .todo-list__label > input::before { display: block; position: absolute; box-sizing: border-box; content: ''; width: 100%; height: 100%; border: 1px solid #333333; border-radius: 2px; transition: 250ms ease-in-out box-shadow, 250ms ease-in-out background, 250ms ease-in-out border; }.ck-content .todo-list .todo-list__label > input::after { display: block; position: absolute; box-sizing: content-box; pointer-events: none; content: ''; left: calc( var(--ck-todo-list-checkmark-size) / 3 ); top: calc( var(--ck-todo-list-checkmark-size) / 5.3 ); width: calc( var(--ck-todo-list-checkmark-size) / 5.3 ); height: calc( var(--ck-todo-list-checkmark-size) / 2.6 ); border-style: solid; border-color: transparent; border-width: 0 calc( var(--ck-todo-list-checkmark-size) / 8 ) calc( var(--ck-todo-list-checkmark-size) / 8 ) 0; transform: rotate(45deg); }.ck-content .todo-list .todo-list__label > input[checked]::before { background: #26ab33; border-color: #26ab33; }.ck-content .todo-list .todo-list__label > input[checked]::after { border-color: #ffffff; }.ck-content .todo-list .todo-list__label .todo-list__label__description { vertical-align: middle; }.ck-content span[lang] { font-style: italic; }.ck-content pre { padding: 1em; color: #353535; background: #c7c7c7; border: 1px solid #c4c4c4; border-radius: 2px; text-align: left; direction: ltr; tab-size: 4; white-space: pre-wrap; font-style: normal; min-width: 200px; }.ck-content pre code { background: unset; padding: 0; border-radius: 0; }.ck-content .mention { background: var(--ck-color-mention-background); color: var(--ck-color-mention-text); }

Hi, in PG-14, how do I delete the keys "dunsNumber": "NaN":

 

{  "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6",  "details": [    {      "keyInformation": {        "dunsNumber": "NaN",        "organizationType": "LIMITED_COMPANY"      }    },    {      "keyInformation": {        "dunsNumber": "123",        "organizationType": "LIMITED_COMPANY"      }    }  ],  "nisse": 123
}

 

So that the result becomes:

 

{  "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6",  "details": [    {      "keyInformation": {        "organizationType": "LIMITED_COMPANY"      }    },    {      "keyInformation": {        "dunsNumber": "123",        "organizationType": "LIMITED_COMPANY"      }    }  ],  "nisse": 123
}

 

Thanks.

 

--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
Attachment

Re: Delete values from JSON

From
Andreas Joseph Krogh
Date:
På fredag 17. mars 2023 kl. 11:56:22, skrev Romain MAZIÈRE <romain.maziere@sigmaz-consilium.fr>:

Hi,

If it is jsonb type, you can have a look at the documentation : https://www.postgresql.org/docs/14/functions-json.html

There are some examples :

jsonb - textjsonb

Deletes a key (and its value) from a JSON object, or matching string value(s) from a JSON array.

'{"a": "b", "c": "d"}'::jsonb - 'a'{"c": "d"}

'["a", "b", "c", "b"]'::jsonb - 'b'["a", "c"]

jsonb - text[]jsonb

Deletes all matching keys or array elements from the left operand.

'{"a": "b", "c": "d"}'::jsonb - '{a,c}'::text[]{}

jsonb - integerjsonb

Deletes the array element with specified index (negative integers count from the end). Throws an error if JSON value is not an array.

'["a", "b"]'::jsonb - 1["a"]

jsonb #- text[]jsonb

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

'["a", {"b":1}]'::jsonb #- '{1,b}'["a", {}]

Regards

I have looked at the docs, but it doesn't, AFAIU, show how to conditionally delete a key based on its value, and leave other keys in the JSONB not matching the value alone.

I want to delete all keys in the (pseudo) path details.keyInformation[*].dunsNumber if the value is "NaN".

 

--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
 
Attachment

Re: Delete values from JSON

From
Boris Zentner
Date:


Am 17.03.2023 um 08:56 schrieb Andreas Joseph Krogh <andreas@visena.com>:

Hi, in PG-14, how do I delete the keys "dunsNumber": "NaN":

 

{  "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6",  "details": [    {      "keyInformation": {        "dunsNumber": "NaN",        "organizationType": "LIMITED_COMPANY"      }    },    {      "keyInformation": {        "dunsNumber": "123",        "organizationType": "LIMITED_COMPANY"      }    }  ],  "nisse": 123
}

 

So that the result becomes:

 

{  "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6",  "details": [    {      "keyInformation": {        "organizationType": "LIMITED_COMPANY"      }    },    {      "keyInformation": {        "dunsNumber": "123",        "organizationType": "LIMITED_COMPANY"      }    }  ],  "nisse": 123
}

 

Thanks.

Hi Andreas, this works for me.

➤ 2023-03-18 14:23:51 CET bz@[local]:5432/test
=# WITH data(j)
  AS (VALUES (CAST('{ "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6", "details": [ { "keyInformation": { "dunsNumber": "NaN", "organizationType": "LIMITED_COMPANY" } }, { "keyInformation": { "dunsNumber": "123", "organizationType": "LIMITED_COMPANY" } } ], "nisse": 123 }' AS jsonb)))

  SELECT jsonb_pretty(jsonb_set(j
                              , '{details}'
                              , (SELECT jsonb_agg(CASE
                                                    WHEN ((elem -> 'keyInformation') ->> 'dunsNumber') = 'NaN'
                                                      THEN jsonb_set(elem
                                                                   , '{keyInformation}'
                                                                   , (elem -> 'keyInformation') - 'dunsNumber')
                                                    ELSE elem
                                                  END)
                                 FROM jsonb_array_elements(data.j -> 'details') AS elem))) AS nice_output
  FROM data
;
nice_output
{
    "nisse": 123,
    "details": [
        {
            "keyInformation": {
                "organizationType": "LIMITED_COMPANY"
            }
        },
        {
            "keyInformation": {
                "dunsNumber": "123",
                "organizationType": "LIMITED_COMPANY"
            }
        }
    ],
    "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6"
}
(1 row)
Time: 0,731 ms

--
Boris


Re: Delete values from JSON

From
Andreas Joseph Krogh
Date:
Excellent!
Thanks!
 
På lørdag 18. mars 2023 kl. 14:26:57, skrev Boris Zentner <bzm@2bz.de>:


 


 

Am 17.03.2023 um 08:56 schrieb Andreas Joseph Krogh <andreas@visena.com>:


 

Hi, in PG-14, how do I delete the keys "dunsNumber": "NaN":

 

{  "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6",  "details": [    {      "keyInformation": {        "dunsNumber": "NaN",        "organizationType": "LIMITED_COMPANY"      }    },    {      "keyInformation": {        "dunsNumber": "123",        "organizationType": "LIMITED_COMPANY"      }    }  ],  "nisse": 123
}

 

So that the result becomes:

 

{  "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6",  "details": [    {      "keyInformation": {        "organizationType": "LIMITED_COMPANY"      }    },    {      "keyInformation": {        "dunsNumber": "123",        "organizationType": "LIMITED_COMPANY"      }    }  ],  "nisse": 123
}

 

Thanks.


 

Hi Andreas, this works for me.
 
➤ 2023-03-18 14:23:51 CET bz@[local]:5432/test
=# WITH data(j)
  AS (VALUES (CAST('{ "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6", "details": [ { "keyInformation": { "dunsNumber": "NaN", "organizationType": "LIMITED_COMPANY" } }, { "keyInformation": { "dunsNumber": "123", "organizationType": "LIMITED_COMPANY" } } ], "nisse": 123 }' AS jsonb)))
 
  SELECT jsonb_pretty(jsonb_set(j
                              , '{details}'
                              , (SELECT jsonb_agg(CASE
                                                    WHEN ((elem -> 'keyInformation') ->> 'dunsNumber') = 'NaN'
                                                      THEN jsonb_set(elem
                                                                   , '{keyInformation}'
                                                                   , (elem -> 'keyInformation') - 'dunsNumber')
                                                    ELSE elem
                                                  END)
                                 FROM jsonb_array_elements(data.j -> 'details') AS elem))) AS nice_output
  FROM data
;
nice_output
{
    "nisse": 123,
    "details": [
        {
            "keyInformation": {
                "organizationType": "LIMITED_COMPANY"
            }
        },
        {
            "keyInformation": {
                "dunsNumber": "123",
                "organizationType": "LIMITED_COMPANY"
            }
        }
    ],
    "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6"
}
(1 row)
Time: 0,731 ms
 
--
Boris
 


 

 

 

--
Andreas Joseph Krogh
CTO / Partner - Visena AS
Mobile: +47 909 56 963
 
Attachment