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

352
Visualizações
Variable is undefined after it leaves scope of getJSON function, how to fix?

Here is my code:

var my_json;
$.getJSON("file.json", function(json) {
  my_json = JSON.stringify(json, null, 2);
  my_json = JSON.parse(my_json);
  console.log('in', my_json); // prints json
});
console.log('after', my_json); // prints undefined

I'm trying to load that JSON file, but for some reason the variable my_json becomes undefined the moment it leaves the scope of the getJSON function. When I print my_json in the getJSON, it looks fine and has the JSON info. The console prints this out: in (json object) and after undefined. I am pretty confused about this. The functions fetch and require haven't worked for me either, which is why I'm using this. When I try to use them, I get this: Uncaught ReferenceError: require is not defined

about 4 years ago · Juan Pablo Isaza
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