What is Json Objects ?

What is JSON Objects ?





Hello Readers,

This post will cover topic of JSON objects. We will get to know what is JSON objects and how to perform different action on them etc.

So lets start it.

At very first lets see syntax of JSON object which is given below:- 

1. Object Syntax:- 

     {"name" : "mayur", "age" : 400}

  • JSON objects are surrounded by curly braces {}.
  • JSON objects are written in key value pairs.
  • Key has to be of strings data type when u try to access it using [] brackets. you can use key  without quotes. Whereas values must be a valid JSON data type such as String, number, object, array, boolean or null.
  • In JSON objects we separate key and values by a colon(:).
  • If you wanted to add more key value pairs I just need to separate them by a comma.


2. How to Access JSON Object values:-


  • We can access values of the JSON object either by using dot (.) notation or or by using bracket [] notation.
  • Ex:-  jobj =  {"name" : "mayur", "age" : 400};
                    x = jobj.name:

                    y = jobj["name"];

  • While accessing using brackets we must have to put key in quotes since it is a string.
  • This is not mandatory when we are accessing using dot notation.


3. How to loop JSON object:- 


  • We can look through to JSON object properties by using for in loop which is shown in below example.
  • Ex:- jobj =  {"name" : "mayur", "age" : 400};
                for (x in jobj) {
                  document.getElementById("demo").innerHTML += x;
                  document.getElementById("demo").innerHTML += jobj[x];
                }

  • Here in above example when we are trying to to access json object using bracket notation it will print all the values of keys for that JSON object; but when we are just putting x it will print all the keys of JSON object.


4. Nested JSON objects:-


  • We can have multiple values for multiple JSON objects inside JSON object.
  • What is called as a nested JSON object.
  • In below example I have tried to show how nested JSON objects looks like.

            jObj = {
                      "name":"mayur",
                      "age":400,
                      "cars": {
                      "car1":"Ford",
                      "car2":"BMW",
                      "car3":"Fiat"
                          }
                        }

  • We can access nested JSON objects either by using dot notation or bracket notation.
  • It's shown in below example:-

            y = jObj.cars.car2;
            // or:
            y = jObj.cars["car2"];


5. How to modify the values of JSON object:-


  • We can use dot notation or using bracket notation to modify any values in a JSON object as shown in below examples:-

            jObj.cars.car2 = "IronCar";

            jObj.cars["car2"] = "volvo";


6. How to delete JSON object properties?

  • We can delete properties from a JSON object using delete keyword as shown in below example:-

                delete jObj.cars.car2;

  • This will delete whole property named cars2 from the JSON object.


So this is all about JSON Object in this post.

Hope you will like it.

Thanks.


Comments

Popular Posts