How to read an external local JSON file in JavaScript?

How to read an external local JSON file in JavaScript? - Hello friend inabnomaniiyaha, In the article that you read this time with the title How to read an external local JSON file in JavaScript?, we have prepared this article well for you to read and take information in it. hopefully the contents of the post Artikel Question,what we write you can understand. Alright, happy reading.

Judul : How to read an external local JSON file in JavaScript?
link : How to read an external local JSON file in JavaScript?

Baca juga


How to read an external local JSON file in JavaScript?

 How to read an external local JSON file in JavaScript?

Question JavaScript

I have saved a JSON file in my local system and created a JavaScript file in order to read the JSON file and print data out. Here is the JSON file:

Example json:

{"resource":"A","literals":["B","C","D"]}

Let’s say this is the path of the JSON file: /Users/Documents/workspace/test.json.

Could anyone please help me write a simple piece of code to read the JSON file and print the data in JavaScript?.

How to read an external local JSON file in JavaScript

Answer 1:

For reading the external Local JSON file (data.json) using javascript, first create your data.json file:

data = '[{"name" : "Niroj", "age" : "22"},{"name" : "Dey", "age" : "20"}]';

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="javascrip.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);

Answer 2:
The loading of a .json file from harddisk 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 works also for loading a .html or .txt files, by overriding the mime type parameter to "text/html", "text/plain" etc.


That's the articleHow to read an external local JSON file in JavaScript?

That's it for the article How to read an external local JSON file in JavaScript? this time, hopefully can be useful for all of you. well, see you in another article post.

You are now reading the articleHow to read an external local JSON file in JavaScript? with link addresshttps://inabnonapudyawanabing.blogspot.com/2020/11/how-to-read-external-local-json-file-in.html

0 Response to "How to read an external local JSON file in JavaScript?"

Post a Comment

Tips Tricks for Android Phone

Tips & Tricks for Android Phone is a free android app and Collection of Tips and Tricks related to using your android mobile device lik...