You can use fetch to get the JSON file.
fetch("../yourFile.JSON").then(res => res.json()).then(data => {
//do something with your JSON
});
fetch('https://jsonplaceholder.typicode.com/todos/1') .then(res => res.json()).then(json => {
console.log(json);
});
Newer browsers support the responseType property of the XMLHttpRequest Object and you can set it to 'json' and then get the JSON response with response property of the XMLHttpRequest.
Note: responseType='json' is not supported by IE11
var req = new XMLHttpRequest;
req.responseType = 'json';
req.open('GET', "../yourFile.JSON", true);
req.onload = function() {
var json = req.response;
// do something with your JSON
};
req.send(null);
var req = new XMLHttpRequest;
req.responseType = 'json';
req.open('GET', "https://jsonplaceholder.typicode.com/todos/1", true);
req.onload = function() {
var json = req.response;
console.log(json);
// do something with your JSON
};
req.send(null);
To support older browsers, you can use XMLHttpRequest and JSON.parse to convert the responseText to JSON.
var req = new XMLHttpRequest;
req.overrideMimeType("application/json");
req.open('GET', "../yourFile.JSON", true);
req.onload = function() {
var json = JSON.parse(req.responseText);
//do something with your JSON
};
req.send(null);
var req = new XMLHttpRequest;
req.overrideMimeType("application/json");
req.open('GET', "https://jsonplaceholder.typicode.com/todos/1", true);
req.onload = function() {
var json = JSON.parse(req.responseText);
console.log(json);
//do something with your JSON
};
req.send(null);
fetch()it. Point the url to this file. Theres.json()bit will then parse your json file into a JS object if it's valid json. Then you can access that object in the last.then(), so replaceconsole.log(json)with code that will find the correct question inside the questions array and return the options property from that question object. If you're creating a TODO app, there's hundreds of examples on google: google.com/search?q=javascript+todo+app+tutorial