How to extract json array elements in postgresql -
what want sum 29.0 , 34.65 , group p_id
table: transaction_items column name: debits, p_id column data type: text, text data:
debits
[{"amount":29.0,"description":"fee_type_1"} [{"amount":"34.65","description":"fee_type_1"}
p_id
16 16
i tried using solution mentioned here [how elements json array in postgresql
select transaction_line_items.p_id, each_attribute ->> 'amount' rev from transaction_line_items cross join json_array_elements(to_json(debits)) each_section cross join json_array_elements(each_section -> 'attributes') each_attribute where (each_attribute -> 'amount') not null;
however, got error saying "cannot deconstruct scalar".
can please let me know how parse values looking for?
thank you.
it seems data broken. values of debtits
column not valid json due lack of right square brackets. assuming data should this:
[{"amount":29.0,"description":"fee_type_1"}] [{"amount":"34.65","description":"fee_type_1"}]
the following query want:
select p_id, sum(amount) ( select p_id, (elements->>'amount')::numeric amount transaction_items cross join json_array_elements(debits::json) elements ) sub group p_id;
Comments
Post a Comment