JSON Object

Spread the love

JSON Object

{
 “name”:“Racks”, 
 “age”:30,
 “country”:”usa”,
     “contact”: {
        “mobile”: “12345678”,
       “phone”: ”12121212”
     }
}

More Details about Objects:

  • Identify by curly braces {}.
  • Key/value pairs like “name”:”Racks”.
  • Key and value separated by a colon (:).
  • The comma (,) is used for separated every key & value pairs.
  • Key always String/Text.
  • The value must be string, number, object, array, boolean or null.

Get value from JSON Object in javascript:

You can access the object by a bracket ([]) & Dot (.).
For [] need to add the key into that [].
For. Need to add key after Dot.
Example:
obj = {
  “name” : “Racks”
  “age” : 30,
  “country” : “usa”
};
=> obj.name
Racks
=> obj[“name”]
Racks
Checkout JSON Example.
We also use a nested JSON Object to find out the first example.
In that Contact is another object into our main object.
We can also access that object as same as the above example.
{
  “name”:“Racks”, 
  “age”:30,
  “country”:”usa”,
  “contact”:
   {
      “mobile”: “12345678”,
      “phone”: ”12121212”
   }
}
=> obj.contact
{  “mobile”: “12345678”, “phone”: ”12121212”}
=> obj[“contact”]
{  “mobile”: “12345678”, “phone”: ”12121212”}
Find out more Information about JSON Data.
Click here for Learn How to create a JSON file ?

JSON Object Array:

{
  “name”:“Racks”, 
  “age”:30,
  “country”:”usa”,
  “contact”:
   {
      “mobile”: “12345678”,
      “phone”: ”12121212”
   },
  “pets” :[“Cat”,”Dog”]
}

More Details about JSON Array:

  • Identify by square braces [].
  • List of values like [“Jone”, “Racks”, “Sanko”].
  • The comma (,) is used for separated every value.
  • The value must be string, number, object, array, boolean or null.

Get value from JSON Array in javascript:

You can access the array by a bracket ([]).
{
  “name”:“Racks”, 
  “age”:30,
  “pets” :[“Cat”,”Dog”]
}
=> obj.pets[0]
Cat
You can pass the index to find a particular value.