Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pdWad-0007oq-Gn for pgsql-general@arkaria.postgresql.org; Sat, 18 Mar 2023 13:27:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pdWaa-0002Bf-UD for pgsql-general@arkaria.postgresql.org; Sat, 18 Mar 2023 13:27:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pdWaa-0002BV-Bm for pgsql-general@lists.postgresql.org; Sat, 18 Mar 2023 13:27:16 +0000 Received: from relay2-d.mail.gandi.net ([2001:4b98:dc4:8::222]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pdWaW-00028X-Tt for pgsql-general@lists.postgresql.org; Sat, 18 Mar 2023 13:27:15 +0000 Received: (Authenticated sender: bzm@2bz.de) by mail.gandi.net (Postfix) with ESMTPSA id 2778840002; Sat, 18 Mar 2023 13:27:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2bz.de; s=gm1; t=1679146028; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=naXlTHKy6C7salq/f6b5MFzEGx4fRw29ASDZOPOzxDA=; b=m36A9QHkJ59UySYZqM1J1MZTd693/R0OZ4JlJl6wjO43kGPQ/QAEetdrfDXYHMDgXd4t9P jyCH5fI+sCHSfIUnbFB3wfoQ1Bx5OqaBzU7wrkYhz3y9K5SOHd2/U6X9++zvU5sBq2L24v iZ+zHs1e5avLhFiBL7dHed44SXb6eDW/2VFiEbuHJTIdtakLNmg8dbIuZ3/nCz2pYZuthO 3wRf3hiyZVrkB7C89ekQEA2mbHvYLx7kgr26YlVOI9UK7VWSfZ3pnUT6DO3pTCKKmQPhml vpuyeMl9b5wkmcfJRyhFD+JrdyvdyLQ6E6rm+EoZ0Ok62CgVLPqyv80Ky01X3g== Content-Type: multipart/alternative; boundary="Apple-Mail=_62983360-E3CF-4674-A740-35C5154DAA70" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3731.400.51.1.1\)) Subject: Re: Delete values from JSON From: Boris Zentner In-Reply-To: Date: Sat, 18 Mar 2023 14:26:57 +0100 Cc: pgsql-general@lists.postgresql.org Message-Id: <1EA83440-F7D1-4C5D-B521-206F9DA129B8@2bz.de> References: To: Andreas Joseph Krogh X-Mailer: Apple Mail (2.3731.400.51.1.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_62983360-E3CF-4674-A740-35C5154DAA70 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > Am 17.03.2023 um 08:56 schrieb Andreas Joseph Krogh = : >=20 > Hi, in PG-14, how do I delete the keys "dunsNumber": "NaN": > =20 > { > "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6", > "details": [ > { > "keyInformation": { > "dunsNumber": "NaN", > "organizationType": "LIMITED_COMPANY" > } > }, > { > "keyInformation": { > "dunsNumber": "123", > "organizationType": "LIMITED_COMPANY" > } > } > ], > "nisse": 123 > } > =20 > So that the result becomes: > =20 > { > "sessionId": "ce6fc9d0-5923-4d71-9f7e-ae60f35c45d6", > "details": [ > { > "keyInformation": { > "organizationType": "LIMITED_COMPANY" > } > }, > { > "keyInformation": { > "dunsNumber": "123", > "organizationType": "LIMITED_COMPANY" > } > } > ], > "nisse": 123 > } > =20 > Thanks. Hi Andreas, this works for me. =E2=9E=A4 2023-03-18 14:23:51 CET bz@[local]:5432/test =3D# 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') =3D '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 --Apple-Mail=_62983360-E3CF-4674-A740-35C5154DAA70 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=utf-8

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.

=E2=9E=A4 = 2023-03-18 14:23:51 CET bz@[local]:5432/test
=3D# 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') =3D = '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

<= /div>
= --Apple-Mail=_62983360-E3CF-4674-A740-35C5154DAA70--