Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sivK1-00B7KO-8n for pgsql-sql@arkaria.postgresql.org; Tue, 27 Aug 2024 12:29:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sivJy-006tvJ-6t for pgsql-sql@arkaria.postgresql.org; Tue, 27 Aug 2024 12:29:14 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sivJx-006tvA-Qy for pgsql-sql@lists.postgresql.org; Tue, 27 Aug 2024 12:29:14 +0000 Received: from mail-ot1-x32b.google.com ([2607:f8b0:4864:20::32b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sivJu-001kn4-Qd for pgsql-sql@lists.postgresql.org; Tue, 27 Aug 2024 12:29:13 +0000 Received: by mail-ot1-x32b.google.com with SMTP id 46e09a7af769-70f44b88bf0so799836a34.1 for ; Tue, 27 Aug 2024 05:29:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724761750; x=1725366550; darn=lists.postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=9zSKm6f0/s3JDPK9+X88Ky+S8zOH+u3yxCis6jlpQYw=; b=ShVAQu1OV/Qez7HS7EUp35NCwHBiOi+UT7w3awBr9OKbk7mkAl89yBqfCivso2+3YF FsKoSCtMLEkk9j+4dBVnatavIHSqhZAXLqNLpfof6VMxrqHTvEa9SAYV3Uovw19ouHFt Gfxl4lBdK0YXek/qHeayyt+pEUZJj/zDDL+IhVAp/62BlkgqEdQ78S/JuKGSmYx5LAzc gBvoFMZ2oXWI7mzcmcR1BiY3qPc3j9tzjPSr3dXYXc0ecJEl7JEn0w6bOtRfW/OTe/hI nStBDauq2wZ8HykjMrAbL7LG+WC7XmJXmO4whScQoZf0mvhhheuVHR8INRNxDlnBSYbI O9Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724761750; x=1725366550; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=9zSKm6f0/s3JDPK9+X88Ky+S8zOH+u3yxCis6jlpQYw=; b=EjdVwag6lPBpRccvY/l036wAnrGwgzXKxPUWOKgpwIWat8hB4BbDBs5sda4/rObAXY XNP1y6KGMnOWwpVO6oIkG/3o0p1a7srUeLwlaP7ec2iR5AT/ma+LLVDbDory2G6wW58n eOikODh3H/+VIN9AyPF06SmP5mM1DFetjTK41cMnLgtcSS3IkuYiJckygUMkLaNgoutt +svs8UtILZLAWS8X624XsJwDmyKbecXDTzJ3zvScleCHD6QUg6brtqU/UnPK3NjAxGOa ssREmqCeGKkL8wtiM8/EZp202QaUP2V0Do4/6l7aP3Hgd5t2FwYXNo3HhwrK/M9uC+/i 9CZQ== X-Gm-Message-State: AOJu0YyCdVeeVlwZavJjZWb3eFrkvjj5crnKw2lMcnB878XsXxyMJIou 3cxT56LWrfxDVk2F8/hYnmYxnfEHVr8W29Sw9UdO4bnXVtGB4FDqXQrXu29+8iqOTCHl/3BD5Hc R1sPjdyVUAb8mpQFN1Vjxd4WsGt673qxE X-Google-Smtp-Source: AGHT+IGHMYFPXIhkU+t/RctB7ZogG88se7pX8kmJG0MrDl4ZHgGezbF21hziMRO5/3qeKj4fd/tiK3r6mE+AxgkWCyc= X-Received: by 2002:a05:6808:2288:b0:3da:9fac:85f with SMTP id 5614622812f47-3def3be1205mr872447b6e.7.1724761749748; Tue, 27 Aug 2024 05:29:09 -0700 (PDT) MIME-Version: 1.0 From: Jan Bernitt Date: Tue, 27 Aug 2024 14:28:58 +0200 Message-ID: Subject: object mapping for json/jsonb columns To: pgsql-sql@lists.postgresql.org Content-Type: multipart/alternative; boundary="0000000000006a86790620a9632f" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000006a86790620a9632f Content-Type: text/plain; charset="UTF-8" Hi! I hope this is the right group to ask about SQL questions. I did quite some research but could not find a satisfactory solution so I hope to find it by asking this mailing list. I have a jsonb column which holds objects used as a map. Let's assume something like this {"key1":{...}, "key2":{...}, ...} As you see each value in this object map is itself an object. Let's assume each looks like this {"value": 1, "meta": [...]} Now I want to get rid of the "meta" part. So this might be in a query or as an update where I actually strip the "meta" attribute in a bulk update. The crux is that I don't know any of the keys or that I want to do this for each of them. What seems to be missing is a "map" (projection) function. Let's say I want to remove the attribute using #- it seems that I cannot specify "any name" for the 1. path segment mapproperty #- {*,meta} That does not work as * is not valid for "any name" Similarly, when selecting a path there is [*] for any array element but I could not find a working solution for any name in an object. The only solutions I found were super complicated transformations that map the JSON to DB records, manipulate that to the shape and selection desired to then put it together to a JSON object again. That seems so overly complicated and has to be so much slower than a simple mapping function where I just specify what I want the original value (object) to be mapped to. In my case I would simply extract "value" to get rid of "meta". It seems odd to me that something so simple would not be possible while staying in the JSON(B) world so hopefully I just don't know how. Many thanks Jan --0000000000006a86790620a9632f Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi!

I hope this is the right group to a= sk about SQL questions.

I did quite some research = but could not find a satisfactory solution so I hope to find it by asking t= his mailing list.

I have a jsonb column which hold= s objects used as a map. Let's assume something like this
=C2=A0 =C2=A0 {"key1":{...}, "key2":{...}, = ...}

As you see each value in this object=C2=A0map= is itself an object. Let's assume each looks like this

<= /div>
=C2=A0 =C2=A0 {"value": 1, "meta": [...]}

Now I want to get rid of the "meta" part.

So this might be in a query or as an update where I= actually strip the "meta" attribute in a bulk update. The crux i= s that I don't know any of the keys or that I want to do this for each = of them.

What seems to be missing is a "map&q= uot; (projection) function.

Let's say I want t= o remove the attribute using #- it seems that I cannot specify "any na= me" for the 1. path segment

=C2=A0 =C2=A0mapp= roperty #- {*,meta}

That does not work as * is not valid for "a= ny name"=C2=A0

Similarly, when selecting a pa= th there is [*] for any array element but I could not find a working soluti= on for any name in an object.

The only solutions I found were super = complicated transformations that map the JSON to DB records, manipulate tha= t to the shape and selection desired to then put it together to a JSON obje= ct again. That seems so overly complicated and has to be so much slower tha= n a simple mapping function where I just specify what I want the original v= alue (object) to be mapped to. In my case I would simply extract "valu= e" to get rid of "meta".=C2=A0
It seems odd to me that so= mething so simple would not be possible while staying in the JSON(B) world = so hopefully I just don't know how.

Many thanks
Jan


--0000000000006a86790620a9632f--