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

203
Visualizações
How to merge nested json in snowflake?

object.assign is only perform the direct merge but its not working for nested json. If anyone worked on this, could you please share the steps.

For example,I want to update the phone number and city of the user. City is under the location property. How should i update the value of city?

Example:

const user = {
  name: "Liya",
  phone: 12345,
  location: {
    city: "Camden",
    country: "UK"
  }
};

const updates = {
  name: "David",
  phone: 12345678,
  location: {
    city: "Smithfield"
  }
};

Output should be like this:

console.log(Object.assign({}, user, updates));
{ 
  name: 'Liya',
  phone: 12345678,
  location: {
    country: 'UK',
    city: 'Smithfield'
  }
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I'm assuming the name should be David since that's the name in the updates.

Based on @Han Moe Htet's comment, I used code from Vincent on that response. I used his because it does not require any external libraries, which Snowflake currently does not allow for Javascript UDFs.

There's an important consideration with this code. It uses recursion, and Snowflake UDFs have rather limited stack depth. If you have a highly nested object, it could run out of stack depth.

set USR = $${
  name: "Liya",
  phone: 12345,
  location: {
    city: "Camden",
    country: "UK"
  }
}$$;

set UPDATES = $${
  name: "David",
  phone: 12345678,
  location: {
    city: "Smithfield"
  }
}$$;

create or replace function MERGE_OBJECTS("obj1" object, "obj2" object)
returns object
language javascript
strict immutable 
as
$$

return merge(obj1, obj2);

function merge(current, updates) {
  for (key of Object.keys(updates)) {
    if (!current.hasOwnProperty(key) || typeof updates[key] !== 'object') current[key] = updates[key];
    else merge(current[key], updates[key]);
  }
  return current;
}

$$;

with x as 
(
    select parse_json($USR) as USR, parse_json($UPDATES) as UPDATES
)
select merge_objects(USR, UPDATES) from X;
about 4 years ago · Juan Pablo Isaza 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