JSON Syntax

A JSON document may contain information separated by the following separators or tokens. 1. JSON name-value pairs example Name-value pairs have a colon between them as in “name” : “value”. JSON names are on the left side of the colon. They need to be wrapped in double quotation …

JSON Syntax

A JSON document may contain information separated by the following separators or tokens.

  1. ":" to separate name from value
  2. "," to separate name-value pairs
  3. "{" and "}" for objects
  4. "[" and "]" for arrays

1. JSON name-value pairs example

Name-value pairs have a colon between them as in "name" : "value".

JSON names are on the left side of the colon. They need to be wrapped in double quotation marks, as in “name” and can be any valid string. Within each object, keys need to be unique.

JSON values are found to the right of the colon. At the granular level, these need to be one of 6 simple data types:

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

Each name-value pair is separated by a comma, so the JSON looks like this:

"name" : "value", "name" : "value", "name": "value"

e.g.

{
    "color" : "Purple",
    "id" : "210"
}

2. JSON object example

A JSON object is a key-value data format that is typically rendered in curly braces. A JSON object looks something like this:

{
	"color" : "Purple",
	"id" : "210",
	"composition" : {
		"R" : 70,
		"G" : 39,
		"B" : 89
	}
}

3. JSON array example

Data can also be 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 can contain values of different data types.

{
	"colors" :
	[
		{
		"color" : "Purple",
		"id" : "210"
		},
		{
		"color" : "Blue",
		"id" : "211"
		},
		{
		"color" : "Black",
		"id" : "212"
		}
	]
}

Comments

Subscribe
Notify of
guest
4 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Secret

Don’t you think JSON syntax is very complicated?

Anony

What is complicated about it? Can you go into detail?

{ … } Denotes object. Left side is “property” or “key” or “name”, whatever helps you remember it. right side is value to it. Heck, you can say left side is variable, right side is value.
var jsonOb = {
“greeting” : “Hello”
}

plus, there are two ways of accessing.
Console.log(jsonOb.greeting); // Hello
Console.log(jsonOb[“greeting”]); // Hello

I personally love accessing via “.”, as its just like a member variable of an object in C#.

Pradeep

No. its easy to understand with less symbols (like in XML). Plain english. One can have preference over other formats but for newbee to data formats, JSON is one quick thing to learn 🙂