For reading the external Local JSON file (data.json) using javascript, first create your data.json file:
data = '[{"name" : "Ashwin", "age" : "20"},{"name" : "Abhinandan", "age" : "20"}]';
Then,
Mention the path of the json file in the script source along with the javascript file
<script type="text/javascript" src="data.json"></script> <script type="text/javascript" src="javascript.js"></script>Get the Object from the json file
var mydata = JSON.parse(data); alert(mydata[0].name); alert(mydata[0].age); alert(mydata[1].name); alert(mydata[1].age);
For reading the external Local JSON file (data.json) using javascript, first create your data.json file:
data = '[{"name" : "Ashwin", "age" : "20"},{"name" : "Abhinandan", "age" : "20"}]';
Then,
Mention the path of the json file in the script source along with the javascript file
<script type="text/javascript" src="data.json"></script> <script type="text/javascript" src="javascript.js"></script>Get the Object from the json file
var mydata = JSON.parse(data); alert(mydata[0].name); alert(mydata[0].age); alert(mydata[1].name); alert(mydata[1].age);
The loading of a .json file from a hard disk is an asynchronous operation, and thus it needs to specify a callback function to execute after the file is loaded.
function readTextFile(file, callback) {
var rawFile = new XMLHttpRequest();
rawFile.overrideMimeType("application/json");
rawFile.open("GET", file, true);
rawFile.onreadystatechange = function() {
if (rawFile.readyState === 4 && rawFile.status == "200") {
callback(rawFile.responseText);
}
}
rawFile.send(null);
}
//usage:
readTextFile("/Users/Documents/workspace/test.json", function(text){
var data = JSON.parse(text);
console.log(data);
});
This function also works for loading a .html or .txt files, by overriding the mime type parameter to "text/html", "text/plain", etc.
Videos
Since it is in the directory data/, You need to do:
file path is '../../data/file.json'
$.getJSON('../../data/file.json', function(data) {
alert(data);
});
Pure JS:
var request = new XMLHttpRequest();
request.open("GET", "../../data/file.json", false);
request.send(null)
var my_JSON_object = JSON.parse(request.responseText);
alert (my_JSON_object.result[0]);
This solution uses an Asynchronous call. It will likely work better than a synchronous solution.
var request = new XMLHttpRequest();
request.open("GET", "../../data/file.json", false);
request.send(null);
request.onreadystatechange = function() {
if ( request.readyState === 4 && request.status === 200 ) {
var my_JSON_object = JSON.parse(request.responseText);
console.log(my_JSON_object);
}
}
Every explaination I see either just copy pastes the JSON-data into the JS file, or they use code that looks like straight up dark magic. There has got to be a simpler way right?
If there is no "simple" way to do it, then what is the way that requires the least explaining, since my brain refuses to use code I don't completely understand.
Thank you!
You can use fetch API to load your data from external JSON file to javascript
fetch("FILENAME.json")
.then(res => res.json())
.then(json => console.log(json));
OR
fetch("FILENAME.json")
.then(res => res.json())
.then(json => {
// Do whatever you want
console.log(json)
return json;
});
Please note that fetch API doesn't work on the local file system. Once the File is available on your server(via serving application locally or deploying to to somewhere), this code will work perfectly.
You have to loop with for in. You can try this code below.
Actually it is too hard to build an output like your expected output, because your data and your expected output is difference.
const nameJson = '{"Name": "name", "Jab1": 2, "Jab1_OS": -14, "Jab2": 2, "Jab2_OS": -16, "Jab3": 3, "Jab3_OS": -29, "Ftilt": 5, "Ftilt_OS": -13, "Uptilt": 5, "Uptilt_OS": -18, "Dtilt": 5, "Dtilt_OS": -15, "Fair": 16,"Fair_OS": -12,"UpB": 3,"UpB_OS": -20,"Bair": 6,"Bair_OS": -2,"Dair": 5,"Dair_OS": -11,"Upair": 4,"Upair_OS": -3}'
const data = JSON.parse(nameJson);
let name = []
for (const key in data) {
if (Object.hasOwnProperty.call(data, key)) {
let toPush = {}
toPush[key] = data[key]
name.push(toPush);
}
}
console.log(name)