Good day. I have a json file stored on my computer, how can I read it and parse it with JS (XMLHttpRequest) to be used further? For example, it is "/home/user/Documents/project/data.json", do I need to add its data to the project page?

function load() { var xhr = new XMLHttpRequest(); xhr.timeout = 1000; xhr.responseType = 'json'; xhr.open('GET', '/home/ilya/Documents/for_job/data'); xhr.send() } console.log(load()); 

Here is the function that gets returned parsing JSON, I just need to get it, at least in order to view it, then use it, but I get 'undefined', and one more thing, the web server is not deployed.

  • And what exactly can not do? Upload file? It seems everything is just in your case. If the file is located outside the site folder, then make a way to “upload” it to the site (via input type = file for example), and then pass the contents through the parser. - alexoander
  • Added some explanations, and code. - Ilya Rogatkin

1 answer 1

I don’t know whether they have already solved the problem on their own or not, but in general, I’ve been here for two days searching the Internet in search of a solution that wouldn’t require a server or use any library and I didn’t find anything. It makes no sense to draw a library for the sake of this issue, so I used the Open Server , I advise you to set up a home server as well, at least for the sake of simplifying such situations.

 var DATA; function getFile (fileName) { var request = new XMLHttpRequest(); request.open('GET', fileName); request.onloadend = function() { parse(request.responseText); } request.send(); } getFile('json/data.json'); //путь к файлу function parse(obj) { DATA = JSON.parse(obj); console.log(DATA); } 
  • Thank you very much, the fact is that I was also looking for a bunch of options, and it just doesn’t work through js, but jQuery though swears at broken xml but outputs! But thanks a lot anyway! - Ilya Rogatkin