I have a table "test" with a json field "description. I want to filter the table and get only the json elements from the "description" thats status = inUse.
Is there a smarter way to split the json elemnts, filter and after thate group it?
select id, json_agg("descriptionElements") from (
select id, json_array_elements(description)::json "descriptionElements" from test
) foo
where "descriptionElements" ->> 'state' = 'inUse'
group by id
order by id asc;
drop table if exists test;
create table test
(
"id" int
, "description" json
);
insert into test ("id", "description") values (1, '[{"name":"Test1","operatorId":"Operator1","timestamp":"2022-09-10T15:54:58+02:00","state":"available","maxPower":{"unit":"watt","value":50.0},"color":"red"},{"name":"Test2","operatorId":"Operator2","timestamp":"2022-09-10T15:54:58+02:00","state":"unknown","maxPower":{"unit":"watt","value":20.0},"color":"yellow"},{"name":"Test3","operatorId":"Operator3","timestamp":"2022-09-10T15:54:58+02:00","state":"inUse","maxPower":{"unit":"watt","value":5.0},"color":"green"}]');
insert into test ("id", "description") values (2, '[{"name":"Test1","operatorId":"Operator1","timestamp":"2022-09-10T15:54:58+02:00","state":"available","maxPower":{"unit":"watt","value":50.0},"color":"red"},{"name":"Test2","operatorId":"Operator2","timestamp":"2022-09-10T15:54:58+02:00","state":"inUse","maxPower":{"unit":"watt","value":20.0},"color":"yellow"},{"name":"Test3","operatorId":"Operator3","timestamp":"2022-09-10T15:54:58+02:00","state":"inUse","maxPower":{"unit":"watt","value":5.0},"color":"green"}]');
insert into test ("id", "description") values (3, '[{"name":"Test1","operatorId":"Operator1","timestamp":"2022-09-10T15:54:58+02:00","state":"available","maxPower":{"unit":"watt","value":50.0},"color":"red"},{"name":"Test2","operatorId":"Operator2","timestamp":"2022-09-10T15:54:58+02:00","state":"unknown","maxPower":{"unit":"watt","value":20.0},"color":"yellow"},{"name":"Test3","operatorId":"Operator3","timestamp":"2022-09-10T15:54:58+02:00","state":"unknown","maxPower":{"unit":"watt","value":5.0},"color":"green"}]');