How to filter json array per each returned row?

后端 未结 1 445
遇见更好的自我
遇见更好的自我 2021-02-14 02:16

I have a table with json field where array of objects is stored. I would like to query this table and for each returned row return only subset of json array objects by filtering

1条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-14 02:55

    You can use json_array_elements to unnest JSON and array_agg to nest it back after filtering. Something like this:

    SELECT t.id, array_to_json(array_agg(j))
    FROM your_table t, json_array_elements(t.jsonColumn) j
    WHERE j->>'field' = 'abc'
    GROUP BY id;
    

    0 讨论(0)
提交回复
热议问题