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

345
Views
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
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!