Web Programming TutorialsLearn About The Basic Syntax Statements in JSON

Learn About The Basic Syntax Statements in JSON

Basic Syntax Statements

In the last chapter, we discussed the characteristics and applications of JSON data format along with its overview and examples. In this chapter, we are going to discuss the basic syntaxes of JSON (JavaScript Object Notation). The syntaxes in JSON are considered as a subset of the JavaScript syntaxes. JSON syntaxes contain the following.

• Data in JSON is always represented as a name and value pairs (e.g., “subject”: “Java Programming”).
• Curly braces are used to hold objects and the ‘:’ (colon) is placed between each name and value pair. Multiple name-value pairs inside the curly braces are separated by `, ` (comma).
• Square brackets are used to hold arrays. The name-value pairs in an array are separated by `, ` (comma).

Lets take a recap on the JSON data format which we used in the last chapter to store the information related to the students for the various courses in a university.

{
	"student": [
		{
			"rollnum": "01",
			"subject": "Java Programming",
			"name": "Mohit Singh",
			"course": "BTech",
			"university": "International University"
		},
		{
			"rollnum": "02",
			"subject": ".Net Programming",
			"name": "Payal Sharma",
			"course": "BTech",
			"university": "International University"
		},
		{
			"rollnum": "03",
			"subject": "Python Programming",
			"name": "Aparajita Jain",
			"course": "MTech",
			"university": "International University"
		},
		{
			"rollnum": "04",
			"subject": "Pearl Programming",
			"name": "Rohan San",
			"course": "MSc.",
			"university": "International University"
		},
		{
			"rollnum": "05",
			"subject": "C++ Programming",
			"name": "Viraat Sehgal",
			"course": "MSc.",
			"university": "International University"
		}
	]
}

Explanation of the JSON data format.
• Student is used as an array as it starts and closes by the Square brackets and holds values for 5 student records.
• Each student record starts and ends with the curly braces. Multiple curly braces within arrays are separated by `, ` (comma).
• Between each curly braces, there are 5 name-value pairs where names are rollnum, subject, name, course, and university. Each student has different values for these 5 keys.
• All above name-value pairs are separated by ‘:’ (colon) and each such pairs within curly braces by `, ` (comma).

Data Structure supported by JSON
JSON data format provides support for the following two types of data structures.
Name-value pairs as collection: – It is the most common type of Data Structure used for JSON which is supported by different programming languages such as JavaScript, C, C++, PHP, Python, PERL, Ruby, Java/J2EE, etc.
Ordered list of values: − It is type of data structure that includes array, vector or sequence, list, etc.

Examples:
1. Let’s take an example for JSON which has the “path” constructor option and JSON Array with objects as elements.

[
	{
		"id": "F1",
		"item_type": "donut",
		"item_name": "Cake",
		"ppu": 0.47,
		"daos": {
			"dao": [
				{
					"id": "D1",
					"type": "Whole Wheat"
				},
				{
					"id": "D2",
					"type": "All purpose flour"
				},
				{
					"id": "D3",
					"type": "9-grains flour"
				},
				{
					"id": "D4",
					"type": "Italian Cheese bread"
				},
				{
					"id": "D5",
					"type": "Honey Oat bread"
				}
			]
		},
		"topping": [
			{
				"id": "T1",
				"type": "None"
			},
			{
				"id": "T2",
				"type": "Glazed"
			},
			{
				"id": "T3",
				"type": "Sugar"
			},
			{
				"id": "T4",
				"type": "Iced Sugar"
			},
			{
				"id": "T5",
				"type": "Chocolate Dip"
			},
			{
				"id": "T6",
				"type": "whole Chocolate"
			},
			{
				"id": "T7",
				"type": "Maple syrup"
			}
		]
	},
	{
		"id": "F2",
		"item_type": "donut",
		"item_name": "Raised",
		"ppu": 0.65,
		"daos": {
			"dao": [
				{
					"id": "D6",
					"type": "Regular"
				}
			]
		},
		"topping": [
			{
				"id": "T1",
				"type": "None"
			},
			{
				"id": "T2",
				"type": "Glazed"
			},
			{
				"id": "T3",
				"type": "Sugar"
			},
			{
				"id": "T4",
				"type": "Iced Sugar"
			},
			{
				"id": "T5",
				"type": "Chocolate Dip"
			},
			{
				"id": "T6",
				"type": "whole Chocolate"
			}
		]
	},
	{
		"id": "F3",
		"item_type": "donut",
		"item_name": "Traditional",
		"ppu": 0.51,
		"daos": {
			"dao": [
				{
					"id": "D6",
					"type": "Regular"
				},
				{
					"id": "D3",
					"type": "9-grains flour"
				},
				{
					"id": "D4",
					"type": "Italian Cheese bread"
				},
				{
					"id": "D5",
					"type": "Honey Oat bread"
				}
			]
		},
		"topping": [
			{
				"id": "T1",
				"type": "None"
			},
			{
				"id": "T2",
				"type": "Glazed"
			},
			{
				"id": "T3",
				"type": "Sugar"
			},
			{
				"id": "T4",
				"type": "Iced Sugar"
			}
		]
	}
]

Conclusion: –
In this chapter, we discussed the JSON syntaxes along with their implementation of it in a bakery shop as an example.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -