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 1uSMSH-006YWg-8P for pgsql-general@arkaria.postgresql.org; Thu, 19 Jun 2025 21:05:53 +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 1uSMSF-00ErMi-9I for pgsql-general@arkaria.postgresql.org; Thu, 19 Jun 2025 21:05:51 +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 1uSMSE-00ErMZ-UN for pgsql-general@lists.postgresql.org; Thu, 19 Jun 2025 21:05:51 +0000 Received: from mail-pj1-x1030.google.com ([2607:f8b0:4864:20::1030]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1uSMSD-0030nj-1E for pgsql-general@postgresql.org; Thu, 19 Jun 2025 21:05:51 +0000 Received: by mail-pj1-x1030.google.com with SMTP id 98e67ed59e1d1-31393526d0dso720898a91.0 for ; Thu, 19 Jun 2025 14:05:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750367147; x=1750971947; darn=postgresql.org; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=Y/blcLnUB6kZMI+u+TAuvIgHvnUtsO7+QDc8B/Veq3M=; b=Nqka+t5uAx08ron4z6etkbjpdgXpLrMedB9TDuudc8NRQbRAcH2SxyDpfbrmH2lQnV x8EKaNMvnl4Vz+3Vfov1z6pKQNgxIh0DBEwZQtyChOHCrJGzr58dmq8eJYwVFuFT3F1U 8oyeDWoVF/Vj5NIA/Go4jldu8e8T2R6CjzwhHCx0Qqdp2uOouNPFbqup+S31ZVyV4rc2 N6usvxeO6xWK/VgKvuzuRYWRJjyHQnnbNfkztp9fBUib2oxEis3YLP78V3af5At+rIQy wn7pXpEqN8VWgB3xZ7dRYm2ByOtgu6EgTlN0ADCljo07f0i6TIxxzNW+CTAqR1tyCHWn 5A0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750367147; x=1750971947; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=Y/blcLnUB6kZMI+u+TAuvIgHvnUtsO7+QDc8B/Veq3M=; b=VN3awtxkKL2VWhTpOk2wh+kU5v3ZekmTuOrM3FnPyUCTT823MCQKDBgnJ4QzaV5b0k G0QF8T1uNL/0gGrnT+ZUJw0v+sdTqyJABai8XYjP2S4NJVahj7w141Xm/UxZjGx1r9Pm XnXk0A2Vr0TYvAVwLoWNzSE4qNviQ3bn03tRLRSPBSOoWouEeGZ0s7R/9aUWugGx65nj ZyIdSW+LKxjAFxbze2RpH89ikD6929sMmNaj7fGBOZq43af59qdPcDINvjcNfNyEICWy RUts03OpeH64ufwEVOqvXJbxTvsRoTKaXKiYT5w4TGkEJQsxoG60SPpXNtkYB7fkz7lW Qs0A== X-Gm-Message-State: AOJu0YxRFkmvXFGPR/AI9K+o1Qt5912FQmaMxtV07FCrM5xjhlk4uB3N cIObLl2uAuKqDLxbSgBGl/LD8QAB1s/jBOppfjyloZ9uORFaf2I1M5HjgHkqBvvAxFhGJqZ3MmC 4hXOJmwFi0Co8h0MUPXnQlu5kmOGrsMzuw5P+ X-Gm-Gg: ASbGncs1h4KD1pq+JXS/BfLAhWVTEesU9hJxVCUdwZHMNf26Zvatyndy7de3T7KqCQ4 aUB68VsenorYJbyNb2xnGTqm370ZypvySyrtmclCEm65BPsktATx5798yxH4QMFx87QsQcAUiow gchlDIvstrXTzSnwFusiJn48qEKDeVe4oDQIIyLdP90S24jDutVg== X-Google-Smtp-Source: AGHT+IHtjvi/HskQFQwII8QeNT0v/D271sEzHAD0e09ev77C8CyqEQwtmFMn6jTJ1x4ZQWD56YP3BKIJF4DnsZMa/8g= X-Received: by 2002:a17:90a:e7c6:b0:312:e744:5b76 with SMTP id 98e67ed59e1d1-3159d9116fdmr948198a91.33.1750367146808; Thu, 19 Jun 2025 14:05:46 -0700 (PDT) MIME-Version: 1.0 From: Phillip Diffley Date: Thu, 19 Jun 2025 23:05:35 +0200 X-Gm-Features: AX0GCFsquynCj88So6YRAKJLNmCNDQbJVAdJHZD7iOVQzsfz4v6v1rB9FvvnTXE Message-ID: Subject: Convert JSON value back to postgres representation To: pgsql-general@postgresql.org Content-Type: multipart/alternative; boundary="0000000000000324cb0637f31c0a" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000000324cb0637f31c0a Content-Type: text/plain; charset="UTF-8" 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? Thank you, Phillip --0000000000000324cb0637f31c0a Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Postgres has a to_jsonb function tha= t will convert a value into its jsonb representation. I am now trying to tu= rn a json value back into its postgres type. I was hoping there would be so= mething like a from_jsonb function that, along with a type hint, could be u= sed as an inverse of to_jsonb, like=C2=A0

from_jso= nb(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.=C2=A0

CREATE TABLE mytable (id int, c= ol1 int[]);
INSERT INTO mytable VALUES (1, '{1, 2, 3}'), = (2, '{3, 4, 5}');
SELECT * from mytable WHERE col1 =3D (s= elect col1 from json_to_record('{"col1": [1, 2, 3]}'::JSO= N) as x(col1 int[]));

Is there a preferred method for t= urning a JSON value back to its postgres representation?

Thank you,
Phillip
--0000000000000324cb0637f31c0a--