JSON in Plain English

json 1024x478 1

JSON is an open standard, lightweight data-interchange format. It stands JavaScript Object Notation.

JSON is extremely common and useful in the modern webs for read and write on a website.  Before JSON, there is XML, which has much overhead and much more difficult to parse than JSON, which is not only easy for humans to read and write, but also easy for computer to parse.

JSON was derived from JavaScript, hence the name JavaScript Object Notation. However, it is a language-independent data format. JSON always have file extension .json. phpGrid also uses JSON for retrieving and updating datagrids without refreshing the entire page.

A simplest JSON contains a key and value:

1
2
3
{
    "color": "red"
}

A JSON key should always have double quotes around it whereas values can have a few of the basic types: string, number, float, boolean, and null. For instance:

1
2
3
4
5
6
7
{
    "String": "Hello",
    "Number": 1,
    "Float": 0.21,
    "Boolean": false,
    "Empty": null
}

The types are explanatory. Non-string values don’t require double quotes. It’s important to use comma after each value.

JSON also can have array as types. Array is made with brackets [ and ] with values in between separated with comma’s. For example:

1
2
3
{
    "colors":[ "red", "blue", "yellow" ]
}

JSON can also have objects . They are defined with curly brackets { and }. Objects as values in JSON must follow the same rules as JSON. In other words, you could have a JSON object inside another JSON object. This is called nesting.

JSON object example:

1
2
3
4
5
6
7
{
    "student":
    {
        "name":"John", 
        "age":18 
    }
}

Even nested JSON objects:

1
2
3
4
5
6
7
8
9
10
11
12
{
    "student":
    {
        "name": "John",
        "age": 18,
        "grades":
        {
            "math": 90,
            "english": 87
        }
    }
}

A complete syntax sample with all the data types discussed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
{
    "firstName": "John",
    "lastName": "Smith",
    "isAlive": true,
    "age": 27,
    "address":
    {
        "streetAddress": "21 2nd Street",
        "city": "New York",
        "state": "NY",
        "postalCode": "10021-3100"
    },
    "phoneNumbers":
    [
        {
            "type": "home",
            "number": "212 555-1234"
        },
        {
            "type": "office",
            "number": "646 555-4567"
        }
    ],
    "children":[],
    "spouse": null
}

Summary

A JSON file is just a text file that stores data in files ended with .json extension. JSON data must conform the following data format:

  • Data is in name/value pairs
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays

To learn more about JSON, I encourage you to check out the official JSON website!

Image Source: json.org