How JSON Indices Work

I had a lot of problems on this subject. I keep trying to figure it out, and I could. But when I actually need it, it doesn't come in my head.
The other day it did come to my head, so I am wrote it down immediately. I described how this works below.


What is JSON?

JSON is short form for JavaScript Object Notation and it is a data serialization format.

In short forms, you can store data with it.

There are stuff that are called Indices, which are used to fetch the data.

Examples for Indices


{
    "name": "Abdullah",

    "hobbies": ["Reading", "Coding", "Cycling"],

    "Working On": "Writing a blog"
}
            

So this is a normal JSON object. Now we will try to get the value of the key "name".

We can do that by:


let object = require("data.json");

let name = object["name"]; // ["name"] gets the value of the key "name". That is what we call *Keys*

console.log(name);

// Outputs: "Abdullah"
            

Above was a snippet of JavaScript code. It gets the JSON data from the file "data.json" and stores it inside object. Then we get the key: "name".

Array Indices

Array Indices work by specifying and index after the array name.

For Example:


console.log(object["hobbies"][0];)

// Outputs: "Reading"
            

Here we get the array name by typing object["hobbies"] and then specify the index.

Remember, the indices always start from 0. So, if we want to get the first element in the array, we can write: object["hobbies"][0].

Conclusion

This is how JSON Indices work.

I know, it is not that hard what it seems, but sometimes it just doesn't come to our heads.