How to read an external local JSON file in JavaScript?

You cannot make a AJAX call to a local resource as the request is made using HTTP. A workaround is to run a local webserver, serve up the file and make the AJAX call to localhost. In terms of helping you write code to read JSON, you should read the documentation for jQuery.getJSON(): http://api.jquery.com/jQuery.getJSON/

await is only valid in async function

The error is not refering to myfunction but to start.  Run code snippetExpand snippet I use the opportunity of this question to advise you about an known anti pattern using await which is : return await. WRONG  Run code snippetExpand snippet CORRECT  Run code snippetExpand snippet Also, know that there is a special case where return await is correct and important : (using … Read more

How to iterate over a JavaScript object?

For most objects, use for .. in : With ES6, if you need both keys and values simultaneously, do To avoid logging inherited properties, check with hasOwnProperty : You don’t need to check hasOwnProperty when iterating on keys if you’re using a simple object (for example one you made yourself with {}). This MDN documentation explains more generally how to deal with objects … Read more

Unexpected token u in JSON at position 0 (but only sometimes)

That unexpected “u” is the first letter of the string “undefined”. It happens because your two asynchronous operations (i.e. loading the JSON and loading the window) are racing to completion, and if the JSON isn’t loaded fast enough, the window.onload method will attempt to parse the JSON string that isn’t loaded yet. A solution is to move … Read more