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 1kmRfd-0006IH-1D for pgsql-general@arkaria.postgresql.org; Tue, 08 Dec 2020 01:20:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kmRfb-0004wz-Oj for pgsql-general@arkaria.postgresql.org; Tue, 08 Dec 2020 01:19:59 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kmRfb-0004ws-Eg for pgsql-general@lists.postgresql.org; Tue, 08 Dec 2020 01:19:59 +0000 Received: from mail-il1-x133.google.com ([2607:f8b0:4864:20::133]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kmRfZ-0003ZP-2B for pgsql-general@lists.postgresql.org; Tue, 08 Dec 2020 01:19:58 +0000 Received: by mail-il1-x133.google.com with SMTP id r17so14046932ilo.11 for ; Mon, 07 Dec 2020 17:19:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=EMQV3IJpwdNePt2xDZG4cTE2qlNqpylQbK9Rryhmt4Y=; b=WDXc5MTTBVudzOWUOy5s/o05nZgJvFvdUZl7NWe2DaKs3sHISh7UHhuSpocs7QFdCi b0CVzXOBfJ7HGElu7MfvDDVxA3hpPMKe0UQ3FcHMsV5dN6U6xbyerj05y4PlCX6jXs75 8A42QT7j0RPnW2tXMrTM+ReuYByY4N0exVenFBljYTh9waE+dk70p4F9JsSuGzNjoYAE /nXNkl1l5yWSCZfHCRUoBBRwm/AsySH9oZQ0IkbVnfbFxae6vkdkWdmHkPIJ0KDePyrv 7C4CmIytAon4y/d3LSaWsszSE/DE596fet0rhdrFrQJC1pYEKHbE8u1it8N2bhfHrASr r/iQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=EMQV3IJpwdNePt2xDZG4cTE2qlNqpylQbK9Rryhmt4Y=; b=U01A2SW6uOhBgXmthsEcjdxhyVNDOqBXQH7AzuuUloPBDsiIWpXv17ssJ2S4932LKJ d1oG9LOywH7Ftl38RDgyajFQKT8XNP92Qo24DcjuCqaI/oanYPCCqYb/91H/bNaajaUv IKN5XRbb861IR6QUlS5UQ0kjA2nQ3NMzKrgMuerBtJTXLok9rtmxOM9QusEUpqfTCaHI oe7Blh65RFk7AO+OBL/FhoDiizNhcGG9m9ZUCBcnIvmd75PqOmsGkcAYLrZzrLPIW+yI DXbxPNimf1/KCjCcBW9kvwZatEJe3rKAQe+AtkKfiHDK27oosIBFNoF9pEvSg2K0i/By /gZQ== X-Gm-Message-State: AOAM533kH96A/VZVscDi+I195RJSloYigOQev/vBAtOXyoc+WRe9CsPB LcfDZ6256p0dLJfloJp74DAdBj0T3sg= X-Google-Smtp-Source: ABdhPJxIoRRu9zL9E9Yc8AL6V1hmwl03TrDIS8B2Vc+lWZqz7JfS1Q8byTMs7bTBEMDyQ0dHiNTY6Q== X-Received: by 2002:a92:845c:: with SMTP id l89mr24050293ild.114.1607390395226; Mon, 07 Dec 2020 17:19:55 -0800 (PST) Received: from ?IPv6:2601:681:5500:dde0:df9:1f95:bb41:a013? ([2601:681:5500:dde0:df9:1f95:bb41:a013]) by smtp.gmail.com with ESMTPSA id s13sm8283891ilu.88.2020.12.07.17.19.54 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 07 Dec 2020 17:19:54 -0800 (PST) Subject: Re: Extracting data from jsonb array? To: "David G. Johnston" Cc: "pgsql-generallists.postgresql.org" References: <72f54672-ad66-0370-9128-206e49389b60@gmail.com> From: Rob Sargent Message-ID: Date: Mon, 7 Dec 2020 18:19:53 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------0F11B3D8A1C54E380006E09F" Content-Language: en-CA List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------0F11B3D8A1C54E380006E09F Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 12/7/20 6:17 PM, David G. Johnston wrote: > On Mon, Dec 7, 2020 at 6:13 PM Rob Sargent > wrote: > > > postgres=# select id, array_agg(fa) from (select id, > (jsonb_array_elements(js)->'key') as fa from foo) g group by id > order by id; >  id |          array_agg > ----+------------------------------ >   1 | {"\"r1kval\"","\"r1kval2\""} >   2 | {"\"r2kval\"","\"r2kval2\""} > (2 rows) > > I think the quotes are a fault of example data? > > The quotes are the fault of the query author choosing the "->" > operator instead of "->>". > > David J. With that correction OP might have an answer? --------------0F11B3D8A1C54E380006E09F Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit


On 12/7/20 6:17 PM, David G. Johnston wrote:
On Mon, Dec 7, 2020 at 6:13 PM Rob Sargent <robjsargent@gmail.com> wrote:


postgres=# select id, array_agg(fa) from (select id, (jsonb_array_elements(js)->'key') as fa from foo) g group by id order by id;
 id |          array_agg          
----+------------------------------
  1 | {"\"r1kval\"","\"r1kval2\""}
  2 | {"\"r2kval\"","\"r2kval2\""}
(2 rows)

I think the quotes are a fault of example data?  

The quotes are the fault of the query author choosing the "->" operator instead of "->>".

David J.
With that correction OP might have an answer?
--------------0F11B3D8A1C54E380006E09F--