0

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

css
design
html
  |____ category.html (this is where the script will be loaded)
js
  |____updateDomScript.js (this is the script that should be loaded)
src
  |____data
          |____jsonFile.json (this is the json that needs to be loaded)

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.

lionrocker221
  • 171
  • 3
  • 8
  • https://stackoverflow.com/questions/7346563/loading-local-json-file –  Dec 30 '20 at 05:31
  • @HamzaKhuswan so I see that requirejs can only load javascript modules and not a JSON file, unless I'm completely missing something, and I don't want to use jQuery. – lionrocker221 Dec 30 '20 at 05:35
  • Look at the second answer if you don't want use `require` –  Dec 30 '20 at 05:41
  • @HamzaKhuswan the second answer uses requirejs, and from what i see that only works on other local javascript modules – lionrocker221 Dec 30 '20 at 05:42
  • How do you host your website? – dee cue Dec 30 '20 at 05:46
  • @deecue im not sure yet but it's all in the same directory and the whole directory will likely be deployed... btw if you have suggestions as to how i should deploy it based on the way it's setup i wouldn't mind – lionrocker221 Dec 30 '20 at 05:50

1 Answers1

1

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.

fetch("path/to/file")
  .then(response => response.json())
  .then(json => console.log(json));