pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Alexander Farber <alexander.farber@gmail.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Extract elements from JSON array and return them as concatenated string
Date: Wed, 14 Mar 2018 10:15:16 -0700
Message-ID: <ea10fde7-e1c0-05b2-97bb-6d53062b069d@aklaver.com> (raw)
In-Reply-To: <2bbe305a-f94f-3385-5313-615260a862d1@aklaver.com>
References: <CAADeyWhC5Tp=mhPU9g2=-q5Y-bU_5_3wGyKLZQrPHscdkG-puA@mail.gmail.com>
	<d1594e9d-1169-9795-4f52-5f9a3f752eec@postgrespro.ru>
	<CAADeyWjfMjO9y5_iNPo7bz8LpgLW8k_7BciR2A2xtVGSdgGnZQ@mail.gmail.com>
	<2bbe305a-f94f-3385-5313-615260a862d1@aklaver.com>

On 03/14/2018 10:12 AM, Adrian Klaver wrote:
> On 03/14/2018 10:02 AM, Alexander Farber wrote:
>> Thank you, Ivan! I am trying to apply your suggestion to my table -
>>
>> On Wed, Mar 14, 2018 at 5:34 PM, Ivan E. Panchenko 
>> <i.panchenko@postgrespro.ru <mailto:i.panchenko@postgrespro.ru>> wrote:
>>
>>     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;
>>
>>
>> # select * from words_moves where gid=656 order by played desc limit 3;
>>   mid  | action | gid | uid  |            played | 
>> tiles                                                 | score
>> ------+--------+-----+------+-------------------------------+------------------------------------------------------------------------------------------------------+------- 
>>
>>   1353 | swap   | 656 |    7 | 2018-03-14 17:22:18.430082+01 | "ЙНРР" 
>> |     ¤
>>   1352 | play   | 656 | 1199 | 2018-03-14 16:55:45.898379+01 | 
>> [{"col": 4, "row": 7, "value": 2, "letter": "С"}, {"col": 5, "row": 7, 
>> "value": 3, "letter": "У"}]   |    19
>>   1351 | play   | 656 |    7 | 2018-03-14 16:38:48.132546+01 | 
>> [{"col": 9, "row": 11, "value": 5, "letter": "Ж"}, {"col": 9, "row": 
>> 13, "value": 2, "letter": "М"}] |    16
>> (3 rows)
>>
>> by trying the following:
>>
>> #  select string_agg(x->>'letter', ' ') from (select 
>> jsonb_array_elements(tiles) from words_moves where gid=656 and 
>> action='play' order by played desc limit 5) x;
>> ERROR:  42883: operator does not exist: record ->> unknown
>> LINE 1: select string_agg(x->>'letter', ' ') from (select jsonb_arra...
>>                             ^
>> HINT:  No operator matches the given name and argument type(s). You 
>> might need to add explicit type casts.
>> LOCATION:  op_error, parse_oper.c:728
>>
>> I am probably missing something obvious?
> 
> Do you still have non-arrays in the tile field?:
> 
> https://www.postgresql.org/message-id/CAADeyWgYKKaArJb6JK_xEtSO%3D7aeNaYqBu_ef-D5W7s8EFPfpQ%40mail.g... 

I should have looked closer before answering, yes there are:

1353 | swap   | 656 |    7 | 2018-03-14 17:22:18.430082+01 | "ЙНРР"

> 
> 
>>
>> Regards
>> Alex
>>
>>
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (7+ messages)  latest in thread

Message-ID: <ea10fde7-e1c0-05b2-97bb-6d53062b069d@aklaver.com>
Permalink:  ../ea10fde7-e1c0-05b2-97bb-6d53062b069d@aklaver.com/
Also on:    postgresql.org/message-id/ea10fde7-e1c0-05b2-97bb-6d53062b069d@aklaver.com

 · 

reply

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: adrian.klaver@aklaver.com, alexander.farber@gmail.com
  Subject: Re: Extract elements from JSON array and return them as concatenated string
  In-Reply-To: <ea10fde7-e1c0-05b2-97bb-6d53062b069d@aklaver.com>
  References: <CAADeyWhC5Tp=mhPU9g2=-q5Y-bU_5_3wGyKLZQrPHscdkG-puA@mail.gmail.com> <d1594e9d-1169-9795-4f52-5f9a3f752eec@postgrespro.ru> <CAADeyWjfMjO9y5_iNPo7bz8LpgLW8k_7BciR2A2xtVGSdgGnZQ@mail.gmail.com> <2bbe305a-f94f-3385-5313-615260a862d1@aklaver.com> <ea10fde7-e1c0-05b2-97bb-6d53062b069d@aklaver.com>

* 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