JavaScript Object Notation (JSON)

JSON is a lightweight data-interchange format that is used to send data between computers or servers. JSON is almost similar to JavaScript Objects except for the fact that the keys in JSON should compulsorily be a string and the value can be of any data type except a function, a date, and undefined. Nested or cascaded JSON are accessed in the same way as JavaScript Objects except for the fact that the key strings that contain spaces, dots, or brackets are only accessible using the JavaScript Object square bracket notation.

Approach 1: Accessing JSON properties using dot notation

Syntax:

jsonObject.property1Name.property2Name...

Like JavaScript Objects, JSON properties can also be accessed using the dot notation in the same manner. In this, the property name provided is compared to the properties of the JSON and if there is a match it returns the corresponding value. If the returned value is another JSON object then its further sub-properties can be accessed in the same manner.

Example: Consider a JSON named myJSON having properties Boss, Department, Department id, and employees array of JSON where the JSON inside employees array consists of name and age. To access the name of the employee at index 1, dot notation can be done as myJSON.employees[1].name and using square bracket notation as myJSON[“employees”][1][“name”].

Javascript
let myJSON = {
    "Boss" : "John",
    "Department" : "Finance",
    "Department id": 3,
    "employees":[
        {
            "name":"Shaun",
            "age": 30
        },
        {
            "name":"Paul",
            "age" : 27
        }
    ]
};

console.log(myJSON.employees[1].name);     
console.log(myJSON["employees"][1]["name"]);

Output
Paul
Paul

Approach 2: Accessing JSON properties using square bracket notation 

Syntax:

jsonObject["property1Name"]["property2Name"]...

Like JavaScript Objects, JSON properties can also be accessed using the square bracket notation in the same manner. In this, the string of the property name provided is compared to the properties of the JSON and if there is a match i.e if the string matches any property string inside the JSON then it returns the corresponding value. If the returned value is another JSON object then its further sub-properties can be accessed in the same manner.

Example: Consider a JSON named myJSON having properties Boss, Department, Department id, and employees array of JSON where the JSON inside employees array consists of name and age. To access the Department id property dot notation cannot be used due to the space between them and hence it becomes compulsory to use square bracket notation to avoid any syntactical error. This can be done as myJSON[“Department id”]

Javascript
let myJSON = {
    "Boss" : "John",
    "Department" : "Finance",
    "Department id": 3,
    "employees":[
        {
            "name":"Shaun",
            "age": 30
        },
        {
            "name":"Paul",
            "age" : 27
        }
    ]
};
   
console.log(myJSON["Department id"]);

Output
3


How to access and process nested objects, arrays, or JSON ?

JavaScript is a scripting and programming language used to add dynamic features to web pages, making them more than just static displays. Often, we need data structures like Objects, Arrays, or JavaScript Object Notation (JSON) to temporarily store data, whether it’s hardcoded or fetched from a database. As data complexity grows, we might organize it into nested structures—like putting one container inside another—for easier access and retrieval.

In this article, we will explore methods for accessing nested JavaScript Objects, Arrays, or JSON. Whether you’re dealing with layers of data, we will show you how to navigate through them effectively.

Similar Reads

Objects

A JavaScript Object is a container that holds the properties of the object in a key-value pair. The properties of an object are accessed using dot notation or square brackets notation. Objects that contain objects inside them are called nested or cascaded Objects. Accessing the value in nested objects can be done by consecutively applying dot or square bracket notation....

Arrays:

JavaScript array is a particular type of container that can hold more than one data of similar or different data types. Values in arrays can be accessed using an index (0-based indexing). Arrays that contain arrays inside them are called nested arrays or cascaded arrays. Accessing the value inside nested arrays can be done by consecutively using indexing in the array....

JavaScript Object Notation (JSON):

JSON is a lightweight data-interchange format that is used to send data between computers or servers. JSON is almost similar to JavaScript Objects except for the fact that the keys in JSON should compulsorily be a string and the value can be of any data type except a function, a date, and undefined. Nested or cascaded JSON are accessed in the same way as JavaScript Objects except for the fact that the key strings that contain spaces, dots, or brackets are only accessible using the JavaScript Object square bracket notation....

Contact Us