How can I open a JSON file in JavaScript without jQuery?

I am writing some code in JavaScript. In this code i want to read a json file. This file will be loaded from an URL.

How can I get the contains of this JSON file in an object in JavaScript?

This is for example my JSON file located at ../json/main.json:

{"mainStore":[{vehicle:'1',description:'nothing to say'},{vehicle:'2',description:'nothing to say'},{vehicle:'3',description:'nothing to say'}]}

and i want to use it in my table.js file like this:

for (var i in mainStore)
    document.write('<tr class="columnHeaders">');
    document.write('<td >'+ mainStore[i]['vehicle'] + '</td>');
    document.write('<td >'+ mainStore[i]['description'] + '</td>');

Here’s an example that doesn’t require jQuery:

function loadJSON(path, success, error)
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function()
        if (xhr.readyState === XMLHttpRequest.DONE) {
            if (xhr.status === 200) {
                if (success)
            } else {
                if (error)
    };"GET", path, true);

Call it as:

         function(data) { console.log(data); },
         function(xhr) { console.error(xhr); }

XHR can be used to open files, but then you’re basically making it hard on yourself because jQuery makes this a lot easier for you. $.getJSON() makes this so easy to do. I’d rather want to call a single line than trying to get a whole code block working, but that’s up to you…

Why i dont want to use jQuery is because the person i am working for doesn’t want it because he is afraid of the speed of the script.

If he can’t properly profile native VS jQuery, he shouldn’t even be programming native code.

Being afraid means he doesn’t know what he is doing. If you plan to go for performance, you actually need to know how to see how to make certain pieces of code faster. If you are only just thinking that jQuery is slow, then you are walking into the wrong roads…

Read More:   Sum similar keys in an array of objects

JSON has nothing to do with jQuery.

There is nothing wrong with the code you have now.

To store the variable mainStore, it is a variable in that json.

You should store that json to a variable:

var myJSON = {"mainStore":[{vehicle:'1',description:'nothing to say'},{vehicle:'2',description:'nothing to say'},{vehicle:'3',description:'nothing to say'}]};

var mainStore = myJSON.mainStore;

//.. rest of your code.

I understand that by “reading a json file” you mean making the request to the url that returns json content. If so, then can you explain why you don’t want to use jQuery for this purpose? It has $.ajax function that is perfectly suitable for this and covers the browsers’ differences.

If you want to read the file then you have to do it server-side, e.g. php and provide it somehow to the dom (there are different methods) so js can use it. Reading file from disk with js is not possible.

function loadDoc() {
    const xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function () {
      if (this.readyState == 4 && this.status == 200) {
    };"GET", "./user.json");

Naming using the linux filename structure
You can store the responseText to a variable or whatever you want to do with it

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts