Answer the question
In order to leave comments, you need to log in
Extract data from json file?
fetch('./data/db.json')
.then((res) => res.json())
.then(res => console.log(res)
Answer the question
In order to leave comments, you need to log in
fetch makes an http request to your server. For it to work, your server must serve this file at the requested url. the file is loaded after the application starts working in the browser.
Judging by the question, this is a dark forest for you, read it, figure it out.
To import, the file must be available at the build stage. then instead of fetch you write import. Then the data from the file is available in the application immediately and is included directly in the application code.
For this to work, you must have a build system set up and json file imports working.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question