Vitaly Teplyakov
Vitaly Teplyakov

Reputation: 51

Modify JSON array element

I have a table

id int | data json

With data:

1 | [1,2,3,2]
2 | [2,3,4]

I want to modify rows to delete array element (int) 2

Expected result:

1 | [1,3]
2 | [3,4]

Upvotes: 2

Views: 519

Answers (1)

klin
klin

Reputation: 121764

As a_horse_with_no_name suggests in his comment the proper data type is int[] in this case. However, you can transform the json array to int[], use array_remove() and transform the result back to json:

with my_table(id, data) as (
values
(1, '[1,2,3,2]'::json),
(2, '[2,3,4]')
)

select id, to_json(array_remove(translate(data::text, '[]', '{}')::int[], 2))
from my_table;

 id | to_json 
----+---------
  1 | [1,3]
  2 | [3,4]
(2 rows)    

Another possiblity is to unnest the arrays with json_array_elements(), eliminate unwanted elements and aggregate the result:

select id, json_agg(elem)
from (
    select id, elem
    from my_table,
    lateral json_array_elements(data) elem
    where elem::text::int <> 2
    ) s
group by 1;

Upvotes: 1

Related Questions