JSON object contains key and value pairs Syntax

{
    key:value
}

Key follows below

  • It must be string or ASCII strings
  • Key’s first character is a letter or underscore(_) and a dollar symbol($)
  • keys are enclosed in double quotes.

value can be of one of the below types

  • object
  • array
  • number
  • string
  • Boolean values are false, true only
  • null

Let’s see some of the examples.

Null JSOn objects

Let’s see an empty json object

{

}

Following is an example for JSOn with key and null value

{ 
    "roles": null 
}

If you want to have an empty collection of values

{ 
    "permissions": [] 
}

JSON with Array of objects example

The object contains key and value pairs and the Array contains a group of objects enclosed in a square bracket[].

{
  "roles": [
    {"name": "admin", "id": 1`},
    {"name": "sales", "id": 2}
  ]
}

2 dimensional Array JSON example

Two-dimensional arrays are an array of arrays. It can be represented as a matrix with rows and columns.

Following is a Two dimensional array JSON example

{
  "grid": [
    [1,2],
    [3,4],
    [5,6],
    [7,8,9]
  ]
}

JSON nested Object example

Nested objects are parent contains child objects.

The object contains a key and the value is again one more object.

Here is an example of JSOn nested object

{
  "JOHN": {
    "name": "john",
    "active": false,
    "age": 30,
    "roles": [
      {
        "name": "admin",
        "id": 1
      },
      {
        "name": "sales",
        "id": 2
      }
    ]
  }
}```
## Sample JSON  Example

Here is a complete JSOn example
```json
{
  "name": "john",
  "active": false,
  "age": 30,
  "roles": [
    {
      "name": "admin",
      "id": 1
    },
    {
      "name": "sales",
      "id": 2
    }
  ],
  "permissions": [
    "READ",
    "WRITE",
    "COPY"
  ],
  "childrens": {
    
  }
}