Loading local JSON file Loading local JSON file json json

Loading local JSON file


$.getJSON is asynchronous so you should do:

$.getJSON("test.json", function(json) {    console.log(json); // this will show the info it in firebug console});


I had the same need (to test my angularjs app), and the only way I found is to use require.js:

var json = require('./data.json'); //(with path)

note: the file is loaded once, further calls will use the cache.

More on reading files with nodejs: http://docs.nodejitsu.com/articles/file-system/how-to-read-files-in-nodejs

require.js: http://requirejs.org/


In a more modern way, you can now use the Fetch API:

fetch("test.json")  .then(response => response.json())  .then(json => console.log(json));

All modern browsers support Fetch API. (Internet Explorer doesn't, but Edge does!)

source: