Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mg1Al-0001ZB-Gg for pgsql-general@arkaria.postgresql.org; Thu, 28 Oct 2021 08:54:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mg1Aj-0008CE-HE for pgsql-general@arkaria.postgresql.org; Thu, 28 Oct 2021 08:54:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mg1Aj-0008B2-6N for pgsql-general@lists.postgresql.org; Thu, 28 Oct 2021 08:54:05 +0000 Received: from mail.tuxteam.de ([5.199.139.25]) by makus.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_128_CBC_SHA1:128) (Exim 4.92) (envelope-from ) id 1mg1Af-0006iU-5A for pgsql-general@lists.postgresql.org; Thu, 28 Oct 2021 08:54:04 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tuxteam.de; s=mail; h=In-Reply-To:Content-Type:MIME-Version:References:Message-ID:Subject:Cc:To:From:Date; bh=cKiRVA+0MMGllBaV9v3ej8mYDPpWDSgw9nIItjlcNh0=; b=LU1gEefZb2mzAF6O/xyDjVCzxLnNILkncGGTak5kLmvvA0LL4WQBml840+XAsDh41TO4pSD0Pdq+xH6hcJrfzCnC9CkbEFKfMB/VICg/ToslojbGnUV8lljVg++dv0vDLXcekX2a63P0EOk1OJCrtJW5V2T5YnSYA8OGPQKjYv7MVra7YDdHxNHbWYlBMmXjTyhzlbPACWb84M1/0JpxGEh5+ZK9Uuec01bcgZUsSBKsv5HYKmFE63fFUdDpA3Z3ERXmkfEHaJgDGxmSiMvEyIY6l76aX8VAGXqTm02QYwvjhvfIA8iplZJSv56sLoRb1jftsg7Qu6yAbHslbR5b+w==; Received: from tomas by mail.tuxteam.de with local (Exim 4.80) (envelope-from ) id 1mg1Ac-0005Vu-CP; Thu, 28 Oct 2021 10:53:58 +0200 Date: Thu, 28 Oct 2021 10:53:58 +0200 From: tomas@tuxteam.de To: "David G. Johnston" Cc: "pgsql-generallists.postgresql.org" Subject: Re: jsonb: unwrapping text Message-ID: <20211028085358.GB17431@tuxteam.de> References: <20211027185807.GA31130@tuxteam.de> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="s/l3CgOIzMHHjg/5" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --s/l3CgOIzMHHjg/5 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Oct 27, 2021 at 04:18:20PM -0700, David G. Johnston wrote: > On Wed, Oct 27, 2021 at 11:58 AM wrote: >=20 > > > > I've found out that one can treat a string singleton as if it > > were an array: > > > > foo=3D# select '"foo"'::jsonb ->> 0; > > ?column? > > ---------- > > foo > > (1 row) > > > > which conveniently returns the right type. My question: can I rely > > on that, or am I missing a much more obvious option? > > > > > Not sure if this exact behavior is trustworthy - but you are on the right > path. Place the value into either a json array or json object and then use > the text versions of the accessor methods to get the json value to pass > through the decoding routine. Thanks a bunch :) I know that, behind the scenes, jsonb scalars (didn't check that for json) are actually represented as one-element arrays, but was unsure how much this can be relied on as "official interface" :-) This leaves us with foo=3D# select jsonb_build_array('"foo"'::jsonb)->>0; ?column?=20 ---------- foo (1 row) =2E..which feels somewhat roundabout, but hey, it actually works. I'll What also seems to work is #>> with an empty path specifier, i.e. select '"foo"'::jsonb #>> '{}'; =2E..but all of them feel somewhat hacky. I'll post a request with the form linked in [1], let's see :-) Thanks again for your assessment, cheers - t --s/l3CgOIzMHHjg/5 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.12 (GNU/Linux) iEYEARECAAYFAmF6ZSYACgkQBcgs9XrR2kYopgCfbmXzHHA8c1PyQJVYXdYTmH3Y xQAAn2NeCokO4Jyh8IvoSVOEghfkn0w1 =i3r3 -----END PGP SIGNATURE----- --s/l3CgOIzMHHjg/5--