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.
Read local JSON file using only Javascript
How to import json files in js
How to import and use data from a JSON file (simply)
Pulling data from local json file
Videos
Hi, is there any way to import .json file in to a js file like (import data from './data.json') ? I've tried that and also added <script type="module"/> in my html also still it was not working for me, I'd greatly appreciate to learn how to import json in my js file.
Thankyou in advance ;)
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!
I have a simple html file:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="Dashboard"> <title>Dashboard</title> </head> <body> <script type="text/javascript" src="update.js"></script> <h1>Price of Stocks:</h1> <h2 id="ap">Apple: </h2> <h2 id="ts">Tesla: </h2> <button onclick="updatePrice();">update</button> </body> </html>
update.js looks like this:
const src = require("./prices.json");
function updatePrice() {
document.getElementById("ap").innerHTML = "Apple: " + src.Apple.price;
document.getElementById("ts").innerHTML = "Tesla: " + src.Tesla.price;
};The json file is as follows: (prices are arbitrarily entered right now)
{
"Apple": {
"price": 10.0
},
"Tesla": {
"price": 10.0
}
}This html file is not on a webserver and loads directly from C://, the prices.json and update.js are located in the same directory as index.html
The main issue here is that I cannot use require outside of node.js, I know I can use browserify, but surely there is a better way to read data from that json file.