3

I want to read a JSON file that in my local machinge using Javescript, and parse it to a Javascript object such as this code:

var parsed = JSON.parse(JSON_file);

Is it possible to read the file from the disk? Or there is other methods to do that. Thanks.

changbenny
  • 378
  • 6
  • 17
  • 2
    your question is not actually clear. What is your question? you need to parse JSON in javascript or you need to read file from disk by javascript? – zahid9i Oct 20 '13 at 19:00
  • 1
    I want to read a JSON file from disk by javascript – changbenny Oct 20 '13 at 19:03
  • this has XY problem written all over it: http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem – ultranaut Oct 20 '13 at 19:04
  • I think this post will help you much http://stackoverflow.com/questions/585234/how-to-read-and-write-into-file-using-javascript – zahid9i Oct 20 '13 at 19:06
  • Are you asking how to read a file, or how to use `JSON` object? In modern browsers there's [`FileReader Api`](https://developer.mozilla.org/en-US/docs/Web/API/FileReader) available, in older IE's you can use some `activeX` to read a file (if this is not a web page), just notice, that `JSON` is supported starting from IE8 on... – Teemu Oct 20 '13 at 19:09

1 Answers1

3

Here is a nice tutorial on how you can do it using HTML5's FileReader API, but there is one constraint: you can only interact with a file selected by the user via a file input field.

http://www.html5rocks.com/en/tutorials/file/dndfiles/

Tri-Edge AI
  • 330
  • 2
  • 15