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 1uSTqv-0086iU-CU for pgsql-general@arkaria.postgresql.org; Fri, 20 Jun 2025 04:59:49 +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 1uSTqs-00GnJ5-FU for pgsql-general@arkaria.postgresql.org; Fri, 20 Jun 2025 04:59:47 +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 1uSTqs-00GnIw-35 for pgsql-general@lists.postgresql.org; Fri, 20 Jun 2025 04:59:46 +0000 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uSTqq-0034FB-0y for pgsql-general@postgresql.org; Fri, 20 Jun 2025 04:59:45 +0000 Received: by mail-ed1-x52a.google.com with SMTP id 4fb4d7f45d1cf-6099d89a19cso3098640a12.2 for ; Thu, 19 Jun 2025 21:59:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1750395582; x=1751000382; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=QccXcxzo94TTsWLjBl5Q6FaEU9rO09+gSZCU8BE08BY=; b=J8k8F/eD29vjCvq8iAvqQIUyB7jNbi1X8hx9VYLrbwb1Zs9JEy5Q7fHzLAOagHo911 oS9DwXVK6dgVpbXPgwUhr7DX9WWqh651uQcdhJHIr2SE1qv0Mzsg8BagSZspeXTj7Wsp KXRpCyLrR/kAjO6TeYw0lX8ERxGmUYez/kYLh6WvSwJMfNKcfRjvjzGPc+mBcHnyIsvn vFFyezO/aTx2EZDvuSuNwBIpNix/X/wDnTZZmoxQ98x7x6ta6B3Xn6i6bIEPsU8tzoYT dv2d7g+JnKhuvkf2730pxwxPll6JC/7sCHmI3S5uya3jCYFTITisr0ptj3/JXkDzx1qI vW7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750395582; x=1751000382; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=QccXcxzo94TTsWLjBl5Q6FaEU9rO09+gSZCU8BE08BY=; b=QfDXP7oB+tDFyC/kELXVWp0cPYgJyVrsRk6yYPomVe0DhzDRBNs8G7J29yg1+KOZkw cu4NKNTdLY2N2NYgLlkorLq4YbxQ6nU+KrvSgggPWRA6pHUeJpa+uAuY43kaYBSwWTeK Xs8ABuiKKmwLJph+Ta0J/+TlWqwDElpuZ/ROaMwC9oUYm+aPlX3Hm/ZY3/dYW18wxTQy wt0DpiEJUSkKQ0K8/ygqyS5eIEENMXOxKN7mUBFXLtMMueRua/DSfwAKmm0bW4sweJOn X5mSGyC2/Tyh3c/Bz2O/c+WezZ2dVvUpiYZ/QHEsEK67zTVZBMaZPouKZaPu5nSo6u13 8C9Q== X-Forwarded-Encrypted: i=1; AJvYcCW8HBAR8Ln7vkTBm0VsEF+ha90HK9v7kkmilAxfxPBT9A4J90pLXq6hAQ4Hgf9eqK6BGXA6o0db1Ci21qag@postgresql.org X-Gm-Message-State: AOJu0YzzgZ23NKkuAtrKELGxvDvsFkvV0IsoCgqZsU95VZYVPsGvIPKy Q4gNz9b4VscM/qcvlOJ39qRJPGh2kFKdp7U7PO2oLM32vMFfFUzBeQ65WxjTT8mg2Ys= X-Gm-Gg: ASbGncs3T8Z8ucntqH4x8zURFpYXxNjlcYyAc6DE9J8KVqU+TD/tUNR96xW9F4GRrAT G6kjbPgUkoXmSrjVkMdA+JdgMnQKBIlhj2rWEcJZBWJ76Cj7dOwcJzotXPvoV6PrGlZniCXXd8o tKFQ5hpEjBkl810yAmQiKitPMgG8LIQ4hDx2koqWG8MihlL6GChEW76I+CR9aqssT57DugRmiqH yEwzf5E58ngXW6vwpr0VBaCqkxfB8dJxtw3K3quew8T3Z8UHLxc4k1znE+s6fMBNY8H5K6yICRi gL2TUQZe3TmKigOyq+mGEPE2QhfT6RBVQWtbSzjfUEZjHmmNBptcilE70Z7jmefC6aMHwUs2XgB l0qqFVPguAkjUvICz X-Google-Smtp-Source: AGHT+IGm9mX+Fn1GmPBv3NAVTK+7Nndcxj2XHjSnTHApqqS4H5OPmWMyaoWNvo2Pzi/zC0uEnF665w== X-Received: by 2002:a05:6402:254a:b0:606:f836:c656 with SMTP id 4fb4d7f45d1cf-60a1d167667mr1476310a12.19.1750395581974; Thu, 19 Jun 2025 21:59:41 -0700 (PDT) Received: from laurenz.albe-K4N0CV00F97414D ([2001:871:255:52d3:3ac8:61ab:b634:342]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-60a18505075sm845596a12.11.2025.06.19.21.59.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Jun 2025 21:59:41 -0700 (PDT) Message-ID: Subject: Re: Convert JSON value back to postgres representation From: Laurenz Albe To: Phillip Diffley , pgsql-general@postgresql.org Date: Fri, 20 Jun 2025 06:59:41 +0200 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2 (3.56.2-1.fc42) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk 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 h= oping there would > be something like a from_jsonb function that, along with a type hint, cou= ld be used as an > inverse of to_jsonb, like=C2=A0 >=20 > from_jsonb(to_jsonb('{1,2,3}'::int[]) as int[] >=20 > 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 th= e WHERE expression > below, but I feel like there might be a better way to do this.=C2=A0 >=20 > 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[])); >=20 > 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. Not quite right, but: CREATE TEMP TABLE arr(a integer[]); SELECT * FROM jsonb_populate_record( NULL::arr, jsonb_build_object('a', to_jsonb(ARRAY[1, 2, 3])) ); a =20 =E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90=E2=95=90= =E2=95=90 {1,2,3} (1 row) Yours, Laurenz Albe