Re: Extracting data from jsonb array?

Поиск
Список
Период
Сортировка
От Ken Tanzer
Тема Re: Extracting data from jsonb array?
Дата
Msg-id CAD3a31VDRAV8_snO9SbxwJa77=aUJJTcsSvCRnc1v5mG8NqDkA@mail.gmail.com
обсуждение исходный текст
Ответ на Re: Extracting data from jsonb array?  ("David G. Johnston" <david.g.johnston@gmail.com>)
Ответы Re: Extracting data from jsonb array?  ("David G. Johnston" <david.g.johnston@gmail.com>)
Список pgsql-general


On Mon, Dec 7, 2020 at 3:22 PM David G. Johnston <david.g.johnston@gmail.com> wrote:
On Mon, Dec 7, 2020 at 4:13 PM Steve Baldwin <steve.baldwin@gmail.com> wrote:
Try:

select _message_body->'Charges'->>'Name' from ...

Not so much..."Charges" is an array so "->>" doesn't do anything useful.

The OP needs to use "json_array_elements" to navigate past the array and get to the next layer of the json where ->>'Name' will then work.


Thank you David.  I had tried that function without much luck.  But with your inspiration, I made progress and got to this:

select _message_exchange_id,jsonb_array_elements(_message_body->'Charges')->>'Name' FROM message_import_court_case WHERE _message_exchange_id = 1296;
 _message_exchange_id |                  ?column?                  
----------------------+--------------------------------------------
                 1296 | Possession Of Burglary Tools
                 1296 | Burglary In The Second Degree (Commercial)
(2 rows)


But what I really want is one line per message, with the charges in an array.  I can't seem to find the right syntax to make this work:

=> select _message_exchange_id,array_agg(jsonb_array_elements(_message_body->'Charges')->>'Name') FROM message_import_court_case WHERE _message_exchange_id = 1296;
ERROR:  column "message_import_court_case._message_exchange_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: select _message_exchange_id,array_agg(jsonb_array_elements(_...
               ^

=> select _message_exchange_id,array_agg(jsonb_array_elements(_message_body->'Charges')->>'Name') FROM message_import_court_case WHERE _message_exchange_id = 1296 GROUP BY 1;
ERROR:  set-valued function called in context that cannot accept a set

=> select _message_exchange_id,(SELECT array_agg(jsonb_array_elements(_message_body->'Charges')->>'Name')) FROM message_import_court_case WHERE _message_exchange_id = 1296 GROUP BY 1;
ERROR:  set-valued function called in context that cannot accept a set

=> select _message_exchange_id,(SELECT array_agg(jsonb_array_elements(_message_body->'Charges')->>'Name')) FROM message_import_court_case WHERE _message_exchange_id = 1296;
ERROR:  column "message_import_court_case._message_exchange_id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: select _message_exchange_id,(SELECT array_agg(jsonb_array_el...

Thanks!

Ken

 

--
AGENCY Software  
A Free Software data system
By and for non-profits
(253) 245-3801

learn more about AGENCY or
follow the discussion.

В списке pgsql-general по дате отправления:

Предыдущее
От: "David G. Johnston"
Дата:
Сообщение: Re: Extracting data from jsonb array?
Следующее
От: "David G. Johnston"
Дата:
Сообщение: Re: Extracting data from jsonb array?