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 1uSU8X-008Air-2X for pgsql-general@arkaria.postgresql.org; Fri, 20 Jun 2025 05:18:01 +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 1uSU8V-00GuX4-4T for pgsql-general@arkaria.postgresql.org; Fri, 20 Jun 2025 05:17:59 +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 1uSU8U-00GuWa-Pr for pgsql-general@lists.postgresql.org; Fri, 20 Jun 2025 05:17:59 +0000 Received: from mail-oo1-xc36.google.com ([2607:f8b0:4864:20::c36]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uSU8T-0030Yj-1U for pgsql-general@postgresql.org; Fri, 20 Jun 2025 05:17:58 +0000 Received: by mail-oo1-xc36.google.com with SMTP id 006d021491bc7-6115f7ebf2fso83018eaf.3 for ; Thu, 19 Jun 2025 22:17:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750396677; x=1751001477; darn=postgresql.org; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=PO7zyhJWTDossWJRLLYjyHw0XegkwdrdncUuP0lgBzU=; b=bSbL+z/BGyRVT7Hf5YjbsouXSw7JcXeUW0c5kgNtjaaKKlO25A3Fkjz82xVaxg9rMd /rJW7H2GIkhTbdxYMP93/TBG8pqBOo/bt01T1vOee9FG7/qxkdaoPwczWWqIn/eYoL2A /Nmk0fYx5YWjpMm5cylfzh/mdMcGm4iDRR7+glB4dt+56KImNLUWC8b4LRrDStstAQMn ah6mAlw0UHSy4huD1aRQo4Cx3BQEWrIiOzVf5siJeJKB1Wo84W01F+e7KxfD/jq6cHiE 2WcGjVZazJMjfmtO6MZZNCl/tDhxEuOVIACY5+yrtyCbA6mIWRrrHpafDM6LXFxA/eC1 7O8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750396677; x=1751001477; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=PO7zyhJWTDossWJRLLYjyHw0XegkwdrdncUuP0lgBzU=; b=NcLA/Hpofwr/txudt0HzyuMZ7rGqznfKEN9hPwTuAvuylL+8k1CNn+8k7uyZWO+QiW JTOhkheYPjbZTX3b+iJWPz1ppM26OEq/ptFq1uTU6vv+ojSkvMi6Rpk03BGkD9SqGKHt hgTPCZlgil/OV5e8VZ4RT6qkfWKOQiw0sNqXRB9s/xlwcpEQFxf4LXDtoyzjEcidXx+5 riIlKZCdqtzQuIMSd7QjZRSl2aEvwTBXA1a+zMCx9RZAA38aCFyP2zn3UjLqOD8tRCpB I89I1A5F8O9T2emr3HZKtJ9Eb/inYjyOi6Ps7MeSde2GX8ewWqal+p2DW5bHrWiMhIkc KvFg== X-Forwarded-Encrypted: i=1; AJvYcCXVCy5TCNZC87ldmeGXkIh9etkTjgjvLURX7tt6tPx1j3k1pGvY8wGqZgVLIisq/dAMOt2mkSi08txB4hid@postgresql.org X-Gm-Message-State: AOJu0YyyO8eWIopGfg/F9Ha7FP04AOgZFvt/9apqSCr49xsJjBeAjkoc WB/XvGdMPQ2b3bs7sRCTI5LQ3ubV7seBEk9wpmgKDBSK65fBTuvbpubEbQq/OwQz9BnzN+6Ykwc q2yf4FSF3Y+0SKWhUxH9JXuLr0+s47og= X-Gm-Gg: ASbGncswr0u8EBKHTxro1AibKcDTprW7Xx5G65GjgMwdzQHSNRff2BmX5qT3rWiX+hM HVChPVk62evZUWdx8hPHZWxxeT71etSKtZmt7zi6SsFZjxIkB46QdqiMthrSr4Xb27ys5Ky7s0Y QnPjSL6bxcyE8nVtHR/xTBah3lWlkS6t+X0QNpbmi7UOnCaHOOwRLT3olvv1T5r9erc9rfiSZC6 xkj X-Google-Smtp-Source: AGHT+IH4R6qRKUk3FPzCAX1hi6BWtz4OB/0wHBBqypeg5mQgrHSBNCw1poWqfb5NUtTdkL7L1X8l/NUORCq9rOzuTlE= X-Received: by 2002:a4a:ee04:0:b0:611:3eb5:7275 with SMTP id 006d021491bc7-6115b733e0amr1044582eaf.0.1750396676633; Thu, 19 Jun 2025 22:17:56 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a05:6802:28a6:b0:5d1:1254:d2a7 with HTTP; Thu, 19 Jun 2025 22:17:55 -0700 (PDT) In-Reply-To: References: From: "David G. Johnston" Date: Thu, 19 Jun 2025 22:17:55 -0700 X-Gm-Features: AX0GCFvW2zjsSHzFvp4iAj0hjHkWIYRLMp-1Y6vb7YjIMtaRGHEsKdJRbr5HR8E Message-ID: Subject: Re: Convert JSON value back to postgres representation To: Laurenz Albe Cc: Phillip Diffley , "pgsql-general@postgresql.org" Content-Type: multipart/alternative; boundary="000000000000207b210637f9fc24" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000207b210637f9fc24 Content-Type: text/plain; charset="UTF-8" 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 was > 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 = (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 envision. > 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. --000000000000207b210637f9fc24 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thursday, June 19, 2025, Laurenz 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.
--000000000000207b210637f9fc24--