pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Boris Zentner <bzm@2bz.de>
To: Andreas Joseph Krogh <andreas@visena.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Delete values from JSON
Date: Sat, 18 Mar 2023 14:26:57 +0100
Message-ID: <1EA83440-F7D1-4C5D-B521-206F9DA129B8@2bz.de> (raw)
In-Reply-To: <VisenaEmail.bc7.d4292f85844d033f.186ee909d72@origo01.app.internal.visena.net>
References: <VisenaEmail.bc7.d4292f85844d033f.186ee909d72@origo01.app.internal.visena.net>



> 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

view thread (5+ messages)  latest in thread

Message-ID: <1EA83440-F7D1-4C5D-B521-206F9DA129B8@2bz.de>
Permalink:  ../1EA83440-F7D1-4C5D-B521-206F9DA129B8@2bz.de/
Also on:    postgresql.org/message-id/1EA83440-F7D1-4C5D-B521-206F9DA129B8@2bz.de

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: bzm@2bz.de, andreas@visena.com
  Subject: Re: Delete values from JSON
  In-Reply-To: <1EA83440-F7D1-4C5D-B521-206F9DA129B8@2bz.de>
  References: <VisenaEmail.bc7.d4292f85844d033f.186ee909d72@origo01.app.internal.visena.net> <1EA83440-F7D1-4C5D-B521-206F9DA129B8@2bz.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox