Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
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]
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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;
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!