Working with JSON for Web Development

This chapter will help you to learn JSON fundamentals, example, syntax, array, object, encode, decode, file, date and date format. You will also be able to learn JSON examples with other technologies such as Java and PHP.

JSON Object


JSON object holds key/value pair. Each key is represented as a string in JSON and value can be of any type. The keys and values are separated by colon. Each key/value pair is separated by comma.

The curly brace { represents JSON object.

Let's see an example of JSON object.

{
   "employee": {
   "name": "sonoo",
   "salary": 56000,
   "married": true
    }
}

In the above example, employee is an object in which "name", "salary" and "married" are the key. In this example, there are string, number and boolean value for the keys.


JSON Object with Strings

The string value must be enclosed within double quote.

{
"name": "sonoo",
"email": "sonoojaiswal1987@gmail.com"
}

JSON Object with Numbers

JSON supports numbers in double precision floating-point format. The number can be digits (0-9), fractions (.33, .532 etc) and exponents (e, e+, e-,E, E+, E-).

{
"integer": 34,
"fraction": .2145,
"exponent": 6.61789e+0
}

JSON Object with Booleans

JSON also supports boolean values true or false.

{
"first": true,
"second": false
}

JSON Nested Object Example

A JSON object can have another object also. Let's see a simple example of JSON object having another object.

{
   "firstName": "Sonoo",
   "lastName": "Jaiswal",
   "age": 27,
   "address" : {
       "streetAddress": "Plot-6, Mohan Nagar",
       "city": "Ghaziabad",
       "state": "UP",
       "postalCode": "201007"
   }
}