Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read JSON file with fetch() in javascript?

Tags:

How can I read local JSON file with fetch function in javascript? I have JSON file with some dump data and one function which read JSON file on server. For example :

readJson () {    console.log(this)    let vm = this    // http://localhost:8080    fetch('/Reading/api/file').then((response) => response.json()).then(json => {        vm.users = json        console.log(vm.users)    }).catch(function () {        vm.dataError = true    }) } 

So, What must to do to read local json file in this fetch function?

like image 941
BRRusev Avatar asked Aug 15 '18 13:08

BRRusev


People also ask

Can you use fetch on a JSON file?

How to Read a JSON File in JavaScript with the Fetch API. One standard method we can use to read a JSON file (either a local file or one uploaded to a server) is with the Fetch API. It uses the same syntax for both. The only difference would be the URL.

How do I fetch data from a JSON file?

The jQuery code uses getJSON() method to fetch the data from the file's location using an AJAX HTTP GET request. It takes two arguments. One is the location of the JSON file and the other is the function containing the JSON data. The each() function is used to iterate through all the objects in the array.

How do I read a JSON file in REST API?

To receive JSON from a REST API endpoint, you must send an HTTP GET request to the REST API server and provide an Accept: application/json request header. The Accept header tells the REST API server that the API client expects JSON.


1 Answers

How can I read local JSON file with fetch function in javascript?

If you're trying to read http://localhost:8080/Reading/api/file

...then what you're doing is correct except you're missing the .ok check (this is such a common mistake I've written a blog post about it). Also, since you're using arrow functions, you don't need to do let vm = this; unless you prefer it; arrow functions close over this. So:

readJson () {    // http://localhost:8080    fetch('/Reading/api/file')    .then(response => {        if (!response.ok) {            throw new Error("HTTP error " + response.status);        }        return response.json();    })    .then(json => {        this.users = json;        //console.log(this.users);    })    .catch(function () {        this.dataError = true;    }) } 

It's important to remember that this is asynchronous; readJson returns before this.users has the value; it will get it later. If you want to know when it gets it, return the promise so calling code can use then on it:

readJson () {    // http://localhost:8080    return fetch('/Reading/api/file')    // ... 

More in the answers to these questions:

  • How do I return the response from an asynchronous call?
  • Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference

If you're trying to read /Reading/api/file from the file system

...then you can't, in at least some browsers, unless you serve the file via a web server process (as you appear to be serving the page. Then you read it via a URL on that server process as shown above.

To read a local file otherwise, the user has to identify the file, either by picking it in an input type="file" or dragging it into a dropzone. Then you'd read it via the File API, not fetch.

like image 171
T.J. Crowder Avatar answered Sep 25 '22 03:09

T.J. Crowder