How to load up a json file in HTML | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

How to load up a json file in HTML

i am trying to store data in a JSON file but i can import/load it in the HTML page. ive already tried 'const file = require("./file.json")' and 'const file = import("./file.json")' but when i open the page, and see inspects menu's console the require code results in "require is not a function" -_- and the import results in "access denied to file by null" or something like that ... can anyone please tell how can i load json file in html....

12th Jun 2018, 8:55 AM
E4GL
E4GL - avatar
3 Answers
0
1) send an XmlHttpRequest for the json file url 2) get the responseText attribute for the request that contain the file body 3) use JSON.parse for parse json text in a js object
12th Jun 2018, 11:36 AM
KrOW
KrOW - avatar
- 1
also tried banging my head against the wall ... didnt work either
12th Jun 2018, 8:56 AM
E4GL
E4GL - avatar
- 1
thanks
12th Jun 2018, 11:37 AM
E4GL
E4GL - avatar