From: Ivan E. Panchenko <i.panchenko@postgrespro.ru>
To: pgsql-general@lists.postgresql.org
Subject: Re: Extract elements from JSON array and return them as concatenated string
Date: Wed, 14 Mar 2018 19:34:55 +0300
Message-ID: <d1594e9d-1169-9795-4f52-5f9a3f752eec@postgrespro.ru> (raw)
In-Reply-To: <CAADeyWhC5Tp=mhPU9g2=-q5Y-bU_5_3wGyKLZQrPHscdkG-puA@mail.gmail.com>
References: <CAADeyWhC5Tp=mhPU9g2=-q5Y-bU_5_3wGyKLZQrPHscdkG-puA@mail.gmail.com>
Hi Alex,
SELECT string_agg(x->>'letter','') FROM json_array_elements(
'[{"col": 7, "row": 12, "value": 3, "letter": "A"}, {"col": 8, "row":
12, "value": 10, "letter": "B"}, {"col": 9, "row": 12, "value": 1,
"letter": "C"}, {"col": 10, "row": 12, "value": 2, "letter": "D"}]'::json
) x;
Regards,
Ivan Panchenko
Postgres Professional
the Russian PostgreSQL Company
14.03.2018 19:27, Alexander Farber пишет:
> Good afternoon,
>
> A PostgreSQL 10.3 table contains JSON data like:
>
> [{"col": 7, "row": 12, "value": 3, "letter": "A"}, {"col": 8, "row":
> 12, "value": 10, "letter": "B"}, {"col": 9, "row": 12, "value": 1,
> "letter": "C"}, {"col": 10, "row": 12, "value": 2, "letter": "D"}]
>
> Please suggest, how to extract only the "letter" values and
> concatenate them to a string like "ABCD"?
>
> I suppose at the end I should use the ARRAY_TO_STRING function, but
> which JSON function to use for extracting the "letter" values to an array?
>
> I keep looking at
> https://www.postgresql.org/docs/10/static/functions-json.html but
> haven't found a good one yet
>
> Thank you
> Alex
>
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: i.panchenko@postgrespro.ru
Subject: Re: Extract elements from JSON array and return them as concatenated string
In-Reply-To: <d1594e9d-1169-9795-4f52-5f9a3f752eec@postgrespro.ru>
References: <CAADeyWhC5Tp=mhPU9g2=-q5Y-bU_5_3wGyKLZQrPHscdkG-puA@mail.gmail.com> <d1594e9d-1169-9795-4f52-5f9a3f752eec@postgrespro.ru>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox