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 1uSfOr-00BOlT-96 for pgsql-general@arkaria.postgresql.org; Fri, 20 Jun 2025 17:19:37 +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 1uSfOp-003CiH-9M for pgsql-general@arkaria.postgresql.org; Fri, 20 Jun 2025 17:19:35 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1uSfOo-003Ci9-Oi for pgsql-general@lists.postgresql.org; Fri, 20 Jun 2025 17:19:35 +0000 Received: from mail-pj1-x1033.google.com ([2607:f8b0:4864:20::1033]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uSfOn-0035hC-1a for pgsql-general@postgresql.org; Fri, 20 Jun 2025 17:19:34 +0000 Received: by mail-pj1-x1033.google.com with SMTP id 98e67ed59e1d1-313154270bbso2209645a91.2 for ; Fri, 20 Jun 2025 10:19:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750439972; x=1751044772; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=27aE/zlLfG+igRARZuM9JSfPXTlfFQU21cneQgbYfSQ=; b=mPuKd0xbs8o08tSdw05FVEFSXj3jf4Y8XB02io5oHyLrT/n/l714fCO7IWho6dLXpC 1ZK8hjPNG05MapabTFGsXpHDmT+52U4OvHUD7dvZ5eeoNFZBBUQM71wa3Ha8sHvnYbpq Hq5ZE8iS/fcWvQn5ZzrbQNN09m1FznBAFPXKJjWOWpoY60+Kgzg8poWarHteJdAroapA vzBYH8AduDkhyzSBqXzLgPVYjduVVjvFt+eONsHOc65ybz7pTZBQciWzUHmiMxhWQqpt 0yCoO2rlHcTkCZpBPeC4qzUrgUGvsJwUXkMRgblMyxtNcH1mUz32MmH0IVMIxKrluo6z zZaA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750439972; x=1751044772; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=27aE/zlLfG+igRARZuM9JSfPXTlfFQU21cneQgbYfSQ=; b=pFQhRW0LS6b8s4W5Brd024Hncfm0OU7vWVI8vlqIzk6Y2d/GoTGT+VeYcshz8sKW7z RzWQAnS9OIP8+zalKe1uDYZAyC4a6OzXrBqcF+u/GXvBEcahkg3/mNZF7hlOextT8+SX x8ZNX1KiQzJ7TmSsKh+8eXvmfNY7nU7zNOu3h2jyt+OIja+akeRCile/Cy+XZhjJmbq5 hTXa8sLMzmMNoz8GOLz6ovtQf7rrTuXQYydsvbhGtWdYqiwnJvp4ulsUC6Of6FbMGXxb bCUjQm0dQ2zw5Bw4IZ934ZwF34Pd6C0ZQpg1Ropr6tcBfaZyl89PqmRxgR9m8HWS6pw+ 0UtQ== X-Forwarded-Encrypted: i=1; AJvYcCUpIByqQ9snTE2v/wkLg6OCvmSET33ZLuTa0yJ1PFYfoVE8ShD5Nx5JLrYsazF2Pkn9fguJVOV2T4ivKPcY@postgresql.org X-Gm-Message-State: AOJu0Yx2rhjivXH6xF+7L6sVkGeNGXy8jA5Few/rr4B+sAh1RzGigp2z 7IxMiqg9dQ2AFs/7jmeaeHnjbxHdSXQCoIHIa4mIFD2G4qLI3sz/byxiuweKaKe/QBKlgEnn92o 0RARVHnXyQJNZHEw5QQ/7GTxmF+ozDaU= X-Gm-Gg: ASbGncv4CmFYtu/D+x6Uw8Ffi2nZcVtZVOWMI3NR5fMNAGRMEImvkDZVQ06CF57IQIW nZLihK98gYDic5+8shTpMM/ubBJBZkiuXNnJGPELAeRb3atBQ6Dw+CGE9R2RC1vHrL7LNvro0YZ chzOWfeiYkNsSP/GFNBrKrn12qW2WFjHlEMZjxrI2G1URI+PVzgA== X-Google-Smtp-Source: AGHT+IF2DcW5HZKwnqKimmEjuCLGL6sf9crtMzvOpqgGkm7vQH069QmaD+U84D/BfEeoCP9tRzBf6Prp0jS0xCD6/po= X-Received: by 2002:a17:90b:1cc3:b0:311:f05b:869a with SMTP id 98e67ed59e1d1-3159d6408cbmr5969916a91.8.1750439972455; Fri, 20 Jun 2025 10:19:32 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Phillip Diffley Date: Fri, 20 Jun 2025 19:19:20 +0200 X-Gm-Features: AX0GCFvnVDpih2xLAXLmmF_5dbjTUEhNWbVgx52gWok6DPu2gayPK0H3UNWfo_o Message-ID: Subject: Re: Convert JSON value back to postgres representation To: "David G. Johnston" Cc: Laurenz Albe , "pgsql-general@postgresql.org" Content-Type: multipart/alternative; boundary="000000000000c249a6063804108d" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000c249a6063804108d Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Good to know. Thank you! On Fri, Jun 20, 2025 at 7:17=E2=80=AFAM David G. Johnston < david.g.johnston@gmail.com> wrote: > On Thursday, June 19, 2025, Laurenz Albe wrote= : > >> On Thu, 2025-06-19 at 23:05 +0200, Phillip Diffley wrote: >> > Postgres has a to_jsonb function that will convert a value into its >> jsonb representation. >> > I am now trying to turn a json value back into its postgres type. I wa= s >> hoping there would >> > be something like a from_jsonb function that, along with a type hint, >> could be used as an >> > inverse of to_jsonb, like >> > >> > from_jsonb(to_jsonb('{1,2,3}'::int[]) as int[] >> > >> > but I do not see a function like this. I was able to convert a json >> value back to its >> > postgres representation using the jsonb_to_record function, as used in >> the WHERE expression >> > below, but I feel like there might be a better way to do this. >> > >> > CREATE TABLE mytable (id int, col1 int[]); >> > INSERT INTO mytable VALUES (1, '{1, 2, 3}'), (2, '{3, 4, 5}'); >> > SELECT * from mytable WHERE col1 =3D (select col1 from >> json_to_record('{"col1": [1, 2, 3]}'::JSON) as x(col1 int[])); >> > >> > Is there a preferred method for turning a JSON value back to its >> postgres representation? >> >> I think jsonb_populate_record() is the closest thing to what you envisio= n. >> > > jsonb_to_record avoids the temporary type. > > select * from jsonb_to_record('{"ia":[1,2,3]}'::jsonb) as r (ia integer[]= ); > > There is a gap for arrays. Scalars you can just cast and composites have > these functions. But no simple/direct way to go from json array to sql > array is presently implemented. > > Though since 17 json_query can apparently do it. > > select pg_typeof( json_query('[1,2,3]'::jsonb, '$' returning integer[]) ) > -> integer[] > > > David J. > > --000000000000c249a6063804108d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Good to know. Thank you!

On Fri, Jun 20, 2025 at 7:17=E2=80= =AFAM David G. Johnston <david.g.johnston@gmail.com> wrote:
On Thursday, June 19, 2025, Laur= enz Albe <= laurenz.albe@cybertec.at> wrote:
On Thu, 2025-06-19 at 23:05 +0200, Phillip Diffley wrote:=
> Postgres has a to_jsonb function that will convert a value into its js= onb representation.
> I am now trying to turn a json value back into its postgres type. I wa= s hoping there would
> be something like a from_jsonb function that, along with a type hint, = could be used as an
> inverse of to_jsonb, like=C2=A0
>
> from_jsonb(to_jsonb('{1,2,3}'::int[]) as int[]
>
> but I do not see a function like this. I was able to convert a json va= lue back to its
> postgres representation using the jsonb_to_record function, as used in= the WHERE expression
> below, but I feel like there might be a better way to do this.=C2=A0 >
> CREATE TABLE mytable (id int, col1 int[]);
> INSERT INTO mytable VALUES (1, '{1, 2, 3}'), (2, '{3, 4, 5= }');
> SELECT * from mytable WHERE col1 =3D (select col1 from json_to_record(= '{"col1": [1, 2, 3]}'::JSON) as x(col1 int[]));
>
> Is there a preferred method for turning a JSON value back to its postg= res representation?

I think jsonb_populate_record() is the closest thing to what you envision.<= br>

jsonb_to_record avoids the temporary type.=

select * from jsonb_to_record('{"ia"= ;:[1,2,3]}'::jsonb) as r (ia integer[]);

There= is a gap for arrays.=C2=A0 Scalars you can just cast and composites have t= hese functions.=C2=A0 But no simple/direct way to go from json array to sql= array is presently implemented.

Though since 17 j= son_query can apparently do it.

select pg_typeof( = json_query('[1,2,3]'::jsonb, '$' returning integer[]) ) -&g= t; integer[]


David J.
--000000000000c249a6063804108d--