Delete values from JSON
Delete values from JSON
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.
Re: Delete values from JSON
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 :
Deletes a key (and its value) from a JSON object, or matching string value(s) from a JSON array.
|
Deletes all matching keys or array elements from the left operand.
|
Deletes the array element with specified index (negative integers count from the end). Throws an error if JSON value is not an array.
|
Deletes the field or array element at the specified path, where path elements can be either field keys or array indexes.
|
Regards
Romain MAZIÈRE romain.maziere@sigmaz-consilium.fr +33.535.545.085 +33.781.46.36.96 https://sigmaz-consilium.fr
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 KroghCTO / Partner - Visena ASMobile: +47 909 56 963
Re: Delete values from JSON
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-text→jsonbDeletes 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[]→jsonbDeletes all matching keys or array elements from the left operand.
'{"a": "b", "c": "d"}'::jsonb - '{a,c}'::text[]→{}
jsonb-integer→jsonbDeletes 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[]→jsonbDeletes 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".
Re: Delete values from JSON
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.
Re: Delete values from JSON
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(CASEWHEN ((elem -> 'keyInformation') ->> 'dunsNumber') = 'NaN'THEN jsonb_set(elem, '{keyInformation}', (elem -> 'keyInformation') - 'dunsNumber')ELSE elemEND)FROM jsonb_array_elements(data.j -> 'details') AS elem))) AS nice_outputFROM 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