JSON

JSON Syntax

JSON Syntax

This section describes the basic JSON syntax. JSON follows JavaScript syntax. Key points to keep in mind while reading a JSON code snippet are as follows:

  1. Data is stored and transmitted in the form of name/value pairs with colon ‘:’ used as separator.
  2. Each data record is separated by commas ‘,’
  3. Each object is enclosed between curly braces {}
  4. Square brackets [] hold list or arrays
  5. The trailing commas and leading zeros in a number are not allowed.
  6. The octal and hexadecimal formats are not permitted. Only decimal system works
  7. The key within the JSON should not be repeated and must be used a string i.e. should be enclosed within the double-quotes.
  8. The Boolean type matches only two special values: true and false 
  9. The NULL values are represented by the null literal.
  10. JSON contains only properties, no methods.
  11. JSON is a strongly typed format i.e. any mistake such as misplaced comma or colon can cause a JSON file not to work. As a programmer, you must validate your json file before using it. There are many freely available JSON validation programs such as JSONLint.
  12. JSON object takes the form of any data type that is valid for inclusion inside JSON irrespective of its base data type. Thus, a single string or number would be valid JSON.
  13. While JavaScript code allows object properties to be unquoted, JSON only allows quoted strings to be used as properties.

Let us look into each type of JSON object. First, let us see name-value pair used in JSON

Key value pair 🡪Name-value pair has a key (called name) on the left and a value (called value) on the right. Both key and value have a colon ':' in between them. For e.g. "name":"Lakshmi" JSON name is always a string and thus should be written within" ". Moreover, each JSON object must have unique key. JSON values can be one of the following 6 simple data types:

  1. array
  2. boolean
  3. object
  4. string
  5. number
  6. null or empty

Please note that there is no native datetime object in JSON. A valid JSON object with simple key value pair is as follows:

{
    "name": "Nitin Jain",
    "e-mail": "nitin.jain@microsoft.com",
    "mobile": 9910064345,
    "address": "176, Gali Batashan, Chawri Bazar, Delhi - 110006"	
JSON object🡪 it is a collection of key-value data items is called a JSON object. For e.g.,
{
    "name": "Nitin Jain",
    "e-mail": "nitin.jain@microsoft.com",
    "mobile": 9910064345,
    "address": 
	{
	    "house no": 176, 
		"street": "Gali Batashan", 
		"locality": "Chawri Bazar", 
		"city": "Delhi",
		"state": "Delhi",
		"pin code": 110006",
        "country": "India"
	}			
}

 

JSON Array 🡪 JSON array If we want to pass more than one objects in one JSON object then data is nested within the JSON by using JavaScript arrays that are passed as a value using square brackets [ ] on either end of its array type. JSON arrays are ordered collections and may contain values of different data types. So it is a heterogeneous type of data structure. For e.g.

{
	"family members":
	[
		{
		"name": "Nitin Jain",
		"e-mail": "nitin.jain@microsoft.com",
		"mobile": 9910064345,
		"address": 
			{
			"house no": 176, 
			"street": "Gali Batashan", 
			"locality": "Chawri Bazar", 
			"city": "Delhi",
			"state": "Delhi",
			"pin code": 110006",
			"country": "India"
			}
		},
		{
		"name": "Radhika Jain",
		"e-mail": "radhika.jain@google.com",
		"mobile": 9718456783,
		"address": 
			{
			"house no": 176, 
			"street": "Gali Batashan", 
			"locality": "Chawri Bazar", 
			"city": "Delhi",
			"state": "Delhi",
			"pin code": 110006",
			"country": "India"
			}
		},
		{
		"name": "Sachit Jain",
		"e-mail": null,
		"mobile": null,
		"address": 
			{
			"house no": 176, 
			"street": "Gali Batashan", 
			"locality": "Chawri Bazar", 
			"city": "Delhi",
			"state": "Delhi",
			"pin code": 110006",
			"country": "India"
			}
		}		
	]	
}