Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eJd5J-0003D9-IE for pgsql-general@arkaria.postgresql.org; Tue, 28 Nov 2017 10:25:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eJd5I-0007Ye-Sy for pgsql-general@arkaria.postgresql.org; Tue, 28 Nov 2017 10:25:48 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eJd5I-0007YV-IR for pgsql-general@lists.postgresql.org; Tue, 28 Nov 2017 10:25:48 +0000 Received: from smtp.nspdc.no ([185.161.76.37]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eJd5F-00012F-0j for pgsql-general@lists.postgresql.org; Tue, 28 Nov 2017 10:25:47 +0000 Date: Tue, 28 Nov 2017 11:25:41 +0100 From: Bjorn T Johansen To: pgsql-general@lists.postgresql.org Subject: Re: jsonb and where clause? Message-ID: <20171128112541.0b35d714@pennywise-btj> In-Reply-To: References: <20171128091807.218d7809@pennywise-btj> Organization: Havleik Consulting X-Mailer: Claws Mail 3.15.1-dirty (GTK+ 2.24.31; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Tue, 28 Nov 2017 11:28:55 +0300 "Ivan E. Panchenko" wrote: > Hi Bjorn, >=20 > 28.11.2017 11:18, Bjorn T Johansen =D0=BF=D0=B8=D1=88=D0=B5=D1=82: > > Hi. > > > > Just starting to look at how to use jsonb columns and I have a question= . I have found out that I can use the following to search for a value > > inside the jsonb column: > > > > select * from orders where info ->> 'customer' =3D 'John Doe' (where= info is the jsonb column) > > > > > > But what if the jsonb column contains an json array, how can I search t= hen? > > > > info -> [ { "customer" : "John Doe" } ] =20 >=20 > If you know the index in array, you can search like > info->0->>'customer' > If you want to search in any array element, you need to use JSQUERY=20 > extension, > see https://github.com/postgrespro/jsquery >=20 > > > > > > btw, using PostgreSQL 9.6 but will be moving to 10 soon. > > > > > > Regards, > > > > BTJ > > =20 > Regards, > Ivan >=20 Thx... :) btw, just managed to use the following sql: select * from orders where info @> '{"recs": [{ "customer":"John Doe"}]}'=20 (changed my json string to info -> { "recs: [ { "customer" : "John Doe" } ]= } And this seems to work but is this the "wrong" way of doing it or? BTJ