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

110
Views
Modificar elemento de matriz JSON

tengo una mesa

 id int | data json

Con datos:

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

Quiero modificar filas para eliminar el elemento de matriz (int) 2

Resultado Esperado:

 1 | [1,3] 2 | [3,4]
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Como sugiere a_horse_with_no_name en su comentario, el tipo de datos adecuado es int[] en este caso. Sin embargo, puede transformar la matriz json en int[], usar array_remove() y transformar el resultado nuevamente en 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)

Otra posibilidad es desensamblar los arreglos con json_array_elements() , eliminar elementos no deseados y agregar el resultado:

 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!