JSON Parse File Path

186,953

Solution 1

Since it is in the directory data/, You need to do:

file path is '../../data/file.json'

$.getJSON('../../data/file.json', function(data) {         
    alert(data);
});

Pure JS:

   var request = new XMLHttpRequest();
   request.open("GET", "../../data/file.json", false);
   request.send(null)
   var my_JSON_object = JSON.parse(request.responseText);
   alert (my_JSON_object.result[0]);

Solution 2

This solution uses an Asynchronous call. It will likely work better than a synchronous solution.

var request = new XMLHttpRequest();
request.open("GET", "../../data/file.json", false);
request.send(null);
request.onreadystatechange = function() {
  if ( request.readyState === 4 && request.status === 200 ) {
    var my_JSON_object = JSON.parse(request.responseText);
    console.log(my_JSON_object);
  }
}

Solution 3

Loading local JSON file

Use something like this

$.getJSON("../../data/file.json", function(json) {
    console.log(json); // this will show the info in firebug console 
    alert(json);
});

Solution 4

var request = new XMLHttpRequest();
request.open("GET","<path_to_file>", false);
request.send(null);
var jsonData = JSON.parse(request.responseText);

This code worked for me.

Share:
186,953

Related videos on Youtube

jagger
Author by

jagger

Updated on September 17, 2021

Comments

  • jagger
    jagger over 2 years

    I'm stuck trying to get the correct path to the local file. I have the following directories:

    Resources ->
       data ->
           file.json
       js ->
         folder ->
            script.js
       html ->
          folder ->
             file1.html
    

    I'm executing script.js from file1.html, with js code:

    var answers = JSON.parse('../../data/file.json');
    alert(answers);
    

    But it doesn't work, even alert is not starting. What is wrong?

    Also I've tried this:

    function readJSON(file) {
        var request = new XMLHttpRequest();
        request.open('GET', file, false);
        request.send(null);
        if (request.status == 200)
            return request.responseText;
    };
    
    var temp = readJSON('../../data/file.json');
    alert(temp);
    

    Alert undefined in this case.

    • Paul S.
      Paul S. almost 11 years
      Alert undefined for me., what was the status, did it produce an error in your console?
    • jagger
      jagger almost 11 years
      Finally, if (request.status == 200) made issues for me. It gives 0 even if it finds the file. If i delete it, all is ok. Thanks for everyone!
  • Paul S.
    Paul S. almost 11 years
    I didn't realise you could pass a URI to JSON.parse and have it perform a REQUEST for you, where is this documented?
  • karthikr
    karthikr almost 11 years
    yeah.. edit was on the way.. guess should have answered after the fact
  • jagger
    jagger almost 11 years
    Actually, i make it in titanium webview, so normally resources should be root path. If i make document.location.pathname and start in browser i will for sure get file:/// ...
  • jagger
    jagger almost 11 years
    for your pure js alert is not starting also
  • Fabien Snauwaert
    Fabien Snauwaert over 10 years
    my_JSON_object.result is undefined. You would access the Javascript created from the JSON file through my_JSON_object directly instead.
  • O. Jones
    O. Jones about 10 years
    Welcome to SO. Good answer. I edited it because it's best when an answer stands alone. Dialogue about other answers is appropriate in comments.
  • Evorlor
    Evorlor about 9 years
    Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check xhr.spec.whatwg.org.
  • Todd
    Todd about 9 years
    @Evorlor Change the false on line 2 to true. Otherwise it's a synchronous operation.
  • Dagrooms
    Dagrooms almost 9 years
    XMLHttpRequest cannot load file:///.../data.json. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.
  • Gaurav
    Gaurav about 8 years
    I get this same XMLHttpRequest error when developing locally. Are there any non-network calls to load the local JSON file and parse it for use within client-side JavaScript?
  • Timo
    Timo about 2 years
    Did not work for me, but solution below with the asynchronous check if the json is ready worked. I get without it an unexpected end of JSON input error which tells me that the json parse starts but does not finish before the next order starts.
  • Timo
    Timo about 2 years
    @Evorlor here is a closer link to what you are writing that one should use async and not sync.
  • Timo
    Timo about 2 years
    .. but does not work for me, see my comments to the other posts.
  • Timo
    Timo about 2 years
    This example is too far away from the question I think. An example should be used without any effort and here, I need l var to work with it. Welcome to SO.
  • Timo
    Timo about 2 years
    .. but does not work for me, see my comments to the other posts. And .. what is the added value here compared to this? Looks like copy-paste. Welcome to SO.