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 1kmRZ8-0005zz-DL for pgsql-general@arkaria.postgresql.org; Tue, 08 Dec 2020 01:13:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kmRZ7-0005dF-Bw for pgsql-general@arkaria.postgresql.org; Tue, 08 Dec 2020 01:13:17 +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 1kmRZ7-0005d7-2R for pgsql-general@lists.postgresql.org; Tue, 08 Dec 2020 01:13:17 +0000 Received: from mail-ot1-x336.google.com ([2607:f8b0:4864:20::336]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kmRZ1-0003V6-VO for pgsql-general@lists.postgresql.org; Tue, 08 Dec 2020 01:13:16 +0000 Received: by mail-ot1-x336.google.com with SMTP id h19so14467973otr.1 for ; Mon, 07 Dec 2020 17:13:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=R/jSKaKQ5QnHveNMy5ff/qYBjkZ+r3ifAPKCiFZgmHI=; b=hq4CtUZhMXv7abaIqMTGyg9M+HvP3dvZ8Fu6a4kx6B+seEnexurfjMPvx3qcFoM+le x/ieOyZFbNAuXKluP+f9d7aZkmuGKuXbDCrfOTmlrA8DOaaCzbVSZWDPp+0/UDkcYx7x 5uEJWAJX0mt4IOMQxNQxitIbSBB+BmUEc7F+d8SxmhIp9wBxiaFAxgDjtCBVtWc5HBew 7goTQ+3YQuTpdNHCkt0fCF8XJHKU3UyTa540kV8j/+0U08j8jzp3ONJYy+5wWIFynZOO LXW/Zju32mziLseMp6+KoNFYn3xreLPducdBaQaORBSM4Sng0CC+gjf56FVGyqWhMYml cRUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=R/jSKaKQ5QnHveNMy5ff/qYBjkZ+r3ifAPKCiFZgmHI=; b=WDe6wRISjkiS8EKkcN6xZB8uGPC5wKGWU7zMl+VRKdsOyPVaHdRDWz0XtOAk5dDQRJ LfFv+7Nejy7C2Z4vutsPtokcOZV/B/KQNhTgpqg9USsA62SdxDr2kMEB5qvLvdMIgZO2 i4e3PAD67vS7heOYpYcDo8fIkfxijZj8t2CbBQPAIU4MRWePWrzYHmg3+23/2ncfLxR9 7x/AS9u6F4Mz5ycwr6Z0GTXIeXOoL9WNNGg9aGk/SOEUJvHLXjwBkHIm3G6+XCpwmxET F0CRJsIeHu8pEVXTDS8iEX4wMVxqTzdP5/jvr16iAHPcGEfx+VXl8Ja1wnRrdW1yEzUf YZHw== X-Gm-Message-State: AOAM5324otugEcnXUJw9mKyo2MCSrUj9uJKOJJZJgYvDsabFz2Y0QXlJ r57HBgRatJRKGWORbrddwQ0WdajVqRk= X-Google-Smtp-Source: ABdhPJws7ZFjqHQqIRWAdD8XfZ7NScRGfM6nMfKipSNKtoQund/odS4hmFSVckQi+VvPqH3s2EKImA== X-Received: by 2002:a9d:802:: with SMTP id 2mr12407420oty.62.1607389990094; Mon, 07 Dec 2020 17:13:10 -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 r15sm3061463otk.38.2020.12.07.17.13.08 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 07 Dec 2020 17:13:09 -0800 (PST) Subject: Re: Extracting data from jsonb array? To: pgsql-general@lists.postgresql.org References: From: Rob Sargent Message-ID: <72f54672-ad66-0370-9128-206e49389b60@gmail.com> Date: Mon, 7 Dec 2020 18:13:06 -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="------------2C53DADC0C950695DF17B1EE" 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. --------------2C53DADC0C950695DF17B1EE Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit > OK, let me try asking again.  (I'm trying to actually get something > that works.)  So given an example like this: > > CREATE TEMP TABLE foo ( >   id INTEGER, >   js  JSONB > ); > > INSERT INTO foo (id,js) VALUES (1, > '[ > {"key":"r1kval","key2":"r1k2val"}, > {"key":"r1kval2","key2":"r1k2val2"} > ]'); > INSERT INTO foo (id,js) VALUES (2, > '[ > {"key":"r2kval","key2":"r2k2val"}, > {"key":"r2kval2","key2":"r2k2val2"} > ]'); > > Can anyone help me with a working query (preferably with the least > cumbersome syntax possible!) that would return these values (the key2 > values) as array text elements: > > id     Agg_val > ----  ------------------------ > 1     {r1k2val,r1k2val2} > 2     {r2k2val,r2k2val} > > (2 rows) 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? Cumbersome is in the eyes of the beholder ;) --------------2C53DADC0C950695DF17B1EE Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit



OK, let me try asking again.  (I'm trying to actually get something that works.)  So given an example like this:

CREATE TEMP TABLE foo (
  id INTEGER,
  js  JSONB
);

INSERT INTO foo (id,js) VALUES (1,
'[
{"key":"r1kval","key2":"r1k2val"},
{"key":"r1kval2","key2":"r1k2val2"}
]');
INSERT INTO foo (id,js) VALUES (2,
'[
{"key":"r2kval","key2":"r2k2val"},
{"key":"r2kval2","key2":"r2k2val2"}
]');

Can anyone help me with a working query (preferably with the least cumbersome syntax possible!) that would return these values (the key2 values) as array text elements:

id     Agg_val
----  ------------------------
1     {r1k2val,r1k2val2}
2     {r2k2val,r2k2val}

(2 rows)


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? 

Cumbersome is in the eyes of the beholder ;)

--------------2C53DADC0C950695DF17B1EE--