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