JSON Objects: JavaScript Object Notation (JSON) is a text-based, human-readable interchange format used for representing simple data structures and objects in web browser-based code. JavaScript objects can only exist within the JavaScript language, so when you are working with data that needs to be accessed by various languages, it is best to refer to JSON.
Rules to declare an object:
- The object is always defined inside the curly brackets { }.
- Objects are written in key pairs.
- The keys must be strings, and their values must be a valid JSON data type. The JSON data types can be number, string, object, boolean, array, or null.
- The keys and values are separated by a colon(“:”).
- Each key or value pair is separated by a comma.
myOrder = {};
Example:
myOrder = { "name of product" : "earbuds", "cost" : "799", "warranty" : "1 year" };
Accessing Object Values:
- The object values can be accessed by using the dot (“.”) notation.
- We can also access objects by using bracket([]) notation.
Example 1: In the below program we are accessing the object using “.” notation.
JavaScript
let myOrder, i; // Object is created with name myOrder myOrder = { "name_of_the_product" : "Earbuds" , "cost" : "799" , "warranty" : "1 year " }; // Accessing for particular detail // from object myOrder i = myOrder.name_of_the_product; // It prints the detail of name // of the product console.log(i); |
Output:
Earbuds
Example 2:
JavaScript
let myOrder, i; myOrder = { "name_of_product" : "Earbuds" , "cost" : "799" , "warranty" : "1 year" }; // Accessing object using [] notation i = myOrder[ "name_of_product" ]; console.log(i); |
Output:
Earbuds
Nesting of objects in JSON: Objects can be nested inside other objects having a unique access path. In the same document, the same field name can occur in objects which are nested. The access name must be unique. In short, the nested objects will be defined or assigned to other objects.
Example:
myOrder = { "name of product" : "earbuds", "cost" : "799", "warranty" : { "warranty1" : "6 months", "warranty2 : "12 months" } };
In the above example, we have declared another object within the object.
Note: We can even access the nested objects using dot(“.”) notation.
Example:
i = myOrder.warranty.warranty2;
or
i = myOrder.warranty[warranty2];
Modify values Of object: To modify the values, we have two ways.
- The values of the object can modify by using a dot(“.”) notation.
- The values of the object can modify by using bracket (“[ ]”) notation.
Example of Dot Notation:
myOrder.warranty.warranty2 = "3 months";
Example of Bracket Notation:
i = myOrder.warranty[warranty2] = "3 months";
Delete Object Properties: We can delete the JSON object properties by using the “delete” keyword.
Example:
delete myOrder.warranty.warranty2;
Looping an Object: Looping can be done in two ways –
- Looping of an object can be done by using a property for-in loop.
- For looping an object we can even use brackets (“[]”) in the for-in loop property.
Example 1:
Javascript
let myOrder, a; myOrder = { "name of product" : "earbuds" , "cost" : "799" , "warranty" : "1 year" }; for (a in myOrder) { // Accessing loop object // using dot notation console.log(myOrder.a); } |
Output:
name of product cost warranty
Example 2: In the below example we are accessing a looping object using bracket[] notation.
JavaScript
let myOrder, a; myOrder = { "name_of_product" : "earbuds" , "cost" : "799" , "warranty" : "1 year" }; for (a in myOrder) { // Accessing object in looping // using bracket notation console.log(myOrder[a]); } |
Output:
earbuds 799 1 year