I have a simple JSON file sitting in a data
folder within the source of my website. In a javascript script I want to read the JSON and update the DOM with the information inside. The file structure of my website is like this
JavaScript
x
10
10
1
css
2
design
3
html
4
|____ category.html (this is where the script will be loaded)
5
js
6
|____updateDomScript.js (this is the script that should be loaded)
7
src
8
|____data
9
|____jsonFile.json (this is the json that needs to be loaded)
10
I obviously can’t use require()
from nodejs
because this is on the client side. I don’t see how FileReader
would work here. All I need to do is read this JSON file from ../src/data/jsonFile.json
.
Advertisement
Answer
I meant the third answer, you can use fetch statement. If you are confused about fetch then I recommend you look it up online first.
JavaScript
1
4
1
fetch("path/to/file")
2
.then(response => response.json())
3
.then(json => console.log(json));
4