2

So here is the thing, I have a json file and inside of that I have an array, and inside of that I have another array.

This is how the json file looks.

{
    "keys": [
        {
            "game": "Counter-Strike: Global Offensive",
            "price": "5",
            "listofkeys": ["5555w-55w12-2d131","231a1-213x1-31313"]
        },
        {
            "game": "Crusader Kings II",
            "price": "20",
            "listofkeys": ["5555w-55w12-2d131","231a1-213x1-31313"]
        }
    ]
}

My questions is, is it possible to do it? Is it possible for me to use this, and then later do something like this:

(filename.json).keys.listofkeys.length

Is it possible to do .length like that here to check how many arrays there are in "list of keys" ?????? I have tried it for myself, and it does not seem to be working. I need confirmation on this though before I continue. If someone could be so kind to answer :)

Thanks!

1
  • because it is an array you still need to use indexing values. This can easily be checked in the browser itself. Create your page, include the json file, using the dev tools you can set breakpoints on the code or use the console to test your code. Commented Dec 25, 2016 at 1:40

2 Answers 2

1

Not exactly.
In your example - the keys key is an Array, which mean it doesn't have a listofkeys attribute/key.

What you can do is check keys[0].listofkeys.length - This will take the first array in the keys key of your json-object and on that array - it will take the listofkeys key and check it's length.

Sign up to request clarification or add additional context in comments.

2 Comments

undefined:6 "listofkeys": ["5555w-55w12-2d131","231a1-213x1-31313"] ^
You have a syntax error. You miss a comma after price": "5" in your json file. It should be price": "5",
1

Yes it is possible to load an external JSON file by using AJAX and then use the JSON.parse method to load that data to a javascript object variable:

data.json

{
    "keys": [
        {
            "game": "Counter-Strike: Global Offensive",
            "price": "5",
            "listofkeys": ["5555w-55w12-2d131","231a1-213x1-31313"]
        },
        {
            "game": "Crusader Kings II",
            "price": "20",
            "listofkeys": ["5555w-55w12-2d131","231a1-213x1-31313"]
        }
    ]
}

Javascript to load JSON file

function loadJSON(callback) {   

    var xobj = new XMLHttpRequest();
        xobj.overrideMimeType("application/json");
    xobj.open('GET', 'data.json', true);
    xobj.onreadystatechange = function () {
          if (xobj.readyState == 4 && xobj.status == "200") {
            // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
            callback(xobj.responseText);
          }
    };
    xobj.send(null);  
}

function check_json_data(json_data) {

    var obj = JSON.parse(json_data);

    console.log(obj);
    console.log("Keys length:", obj.keys.length);
    console.log("List of keys 1 length:", obj.keys[0].listofkeys.length);
    console.log("List of keys 2 length:", obj.keys[1].listofkeys.length);
}

loadJSON(check_json_data);

OUTPUT

enter image description here

See the console output of my example here: http://zikro.gr/dbg/html/json-load/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.