Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

228
Visualizações
Update every value in an array in postgres json

In my postgres database I have json that looks similar to this:

{
    "myArray": [
        {
            "myValue": 1
        },
        {
            "myValue": 2
        },
        {
            "myValue": 3
        }
    ]
}

Now I want to rename myValue to otherValue. I can't be sure about the length of the array! Preferably I would like to use something like set_jsonb with a wildcard as the array index, but that does not seem to be supported. So what is the nicest solution?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You have to decompose a whole jsonb object, modify individual elements and build the object back.

The custom function will be helpful:

create or replace function jsonb_change_keys_in_array(arr jsonb, old_key text, new_key text)
returns jsonb language sql as $$
    select jsonb_agg(case 
        when value->old_key is null then value
        else value- old_key || jsonb_build_object(new_key, value->old_key) 
        end)
    from jsonb_array_elements(arr)
$$;

Use:

with my_table (id, data) as (
values(1, 
'{
    "myArray": [
        {
            "myValue": 1
        },
        {
            "myValue": 2
        },
        {
            "myValue": 3
        }
    ]
}'::jsonb)
)

select 
    id, 
    jsonb_build_object(
        'myArray',
        jsonb_change_keys_in_array(data->'myArray', 'myValue', 'otherValue')
        )
from my_table;

 id |                           jsonb_build_object                           
----+------------------------------------------------------------------------
  1 | {"myArray": [{"otherValue": 1}, {"otherValue": 2}, {"otherValue": 3}]}
(1 row)
over 4 years ago · Santiago Trujillo Relatório

0

Using json functions are definitely the most elegant, but you can get by on using character replacement. Cast the json(b) as text, perform the replace, then change it back to json(b). In this example I included the quotes and colon to help the text replace target the json keys without conflict with values.

CREATE TABLE mytable ( id INT, data JSONB );

INSERT INTO mytable VALUES (1, '{"myArray": [{"myValue": 1},{"myValue": 2},{"myValue": 3}]}');
INSERT INTO mytable VALUES (2, '{"myArray": [{"myValue": 4},{"myValue": 5},{"myValue": 6}]}');

SELECT * FROM mytable;

UPDATE mytable
SET data = REPLACE(data :: TEXT, '"myValue":', '"otherValue":') :: JSONB;

SELECT * FROM mytable;

http://sqlfiddle.com/#!17/1c28a/9/4

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda