JSON Example


1. Key-Value Pairs

JSON uses key-value pairs to express an object's property. In the above example, we tried to represent a Person. This person has some properties like

  • First Name
  • Age
  • Address

Each of these properties have a value associated with it. For instance, the value of First Name is Rohatash. Age also has a value of 35. The rules for writing a Key-Value in JSON are as follows.

  • Key-value pairs are separated by a : (colon)
  • Key is always present in Double Quotes " "
  • Values must be valid JSON data types: string, number, another JSON object, array, boolean or null.

From the above example, a Key-Value pair is FirstName.

"FirstName" : "Rohatash"

As you can see in the figure below:

{"FirstName" : "Rohatash","Age" : 35,"Address" : "Mathura"}

2. JSON Objects Example

In JSON an object is represented by a collection of Key-Value pairs. This collection of Key-Value pairs are grouped using { } (opening and closing curly braces}. The rules for writing an Object in JSON are as follows.

  • Key-Value pairs should be separated by a , (Comma)
  • Each Object should Start with an Opening {(Opening Curly Brace)

Example

{
"FirstName" : "Rohatash",
"Age" : 35,
"Address" : "Mathura"
}

3. JSON Array Example

An array is a collection or list of data as defined in other language. An array in JSON is a group of values that are separated by commas. The rules for writing an Array in JSON are as follows.

  • An Array starts with an opening [(Bracket)
  • An Array ends with a closing](Bracket)
  • Values in the Array are separated by , (Comma)s

Example

{
   "FirstName" : "Rohatash",
   "Age" : "35",
   "Address" : "Mathura"
   "Qualification" : ["MCA", "BTech", "BCA"]
}

Prev Next