JSON Tutorial: JSON Object Using PHP

JSON Tutorial:  JSON Object 


JSON Object Using PHP:  Hi readers this JSON tutorial is all about JSON Object Here in this tutorial we will explain the details about JSON object which includes what is JSON Object Using PHP Syntax, JSON object with Strings, JSON object with Numbers, JSON object with boolean, and JSON nested objects with examples.

JSON Object Using PHP Syntax: 


{"key1": value1 , "key2": value2 , ...}
 e.g: 
{"ID" : 1001, "name" : "Sudhansu" , "age": 24 , "married" : true }

JSON Object starts with the opening curly braces {  and ends with the ending curly braces  }.

JSON Object is stored in the Key/Value Pair format enclosed with curly braces {}.

Each Key and values are separated by a colon (:)

Keys must be any Strings enclosed with double quotation (” “) whereas values can be any valid JSON datatype (string, number, boolean, object, array or null)

Each key/value pair is separated by a comma(,).

In the above example:

KEY:    ID, name, age, married

VALUES: 1001, “Sudhansu”, 24, true.

Note:  Here some values are numeric, String and Boolean datatype.

JSON Object with Strings:


Let us understand the JSON object with Strings by the following example:

{
"ID" : "A001",
"Name": "Awdesh Rai",
"Age": "26"
}

Here you can see all the values are of String Datatype (enclosed with double quotes).

JSON Object Using PHP with Numbers:


Number datatype in JSON is similar to Integer or int or numeric in any other languages.

JSON number datatype can be any (0-9) digits, fractions (0.33, 0.98, etc.) and exponents (e, e+, e-, E, E+, E-) .

JSON also support floating-point format i.e., Numbers with double precision.

Simple example:

{
"ID" : 1001,
"height": 67.5,
"width": 26.35,
"exp"  : 6.617e+1
}

Here you can see all the values are of Number Datatype (digits, fraction or exponent form).

JSON Object Using PHP with Boolean:


Boolean datatype in JSON is similar to the Boolean datatype in any other languages.

JSON Boolean data type can be either “true” or “false”.

Simple example:

{
"value1" : true,
"value2" : false
}

Here you can see Boolean datatype have values either true or false.

JSON Object with another Object: (Nested Object)


JSON Object can be nested as well. Here Nesting means one or more object can be used within another object.

Let us understand the Nested JSON objects by the following example:

{ //start of object1

 "studentId": "SJ011MS",
 "name": "Deepak Singh",
 "gender": "Male",
 "age": "27",
 "address": 
 { //start of object2
 "city": "varanasi",
 "state": "Uttar Pradesh",
 "pincode": "221001"
  },
 //end of object2
 "course": "B.S.c",
 "section": "B"
}
//end of object1


Here in the above example, you can see that the address object is used within another object.


Let us understand the Nested Object with another example:

{
"student1" :{
"ID" : "A001",
"Name": "Awdesh Rai",
"Age": "26"
 }

"student2" :{ 
"ID" : "A002",
"Name": "Subham Mishra",
"Age": "33"
  }
}

Here we can see that we have used two JSON object i.e, student1 information and student2 information which is nested within another JSON Object.

 


 Previous Topic                                                                     Next Topic

 JSON and XML                                                                  JSON Array

Leave a Comment