What is JSON and Understanding JSON syntax with simple example

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages.

JSON is built on two structures

  • A collection of name/value pairs.
  • An ordered list of values.

Object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).

Example Object – a container of key-value pairs, e.g : { “foo”:”bar”, “x”:0.3 }

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

Example Array – a sequence of values, e.g.: [ 1, 2, 3 ]

String is a sequence of zero or more Unicode characters, wrapped in double quotes, using backslash escapes. A character is represented as a single character string. A string is very much like a C or Java string.

Example String – a quoted sequence of chars, e.g.: “foo”

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array. These structures can be nested.

Example value – a number e.g 78, a boolean (true, false) or null

Let’s consider a JSON string:

{
	"user": "johndoe",
	"admin": false,
	"uid": 1000,
	"groups": ["users", "wheel", "audio", "video"]
}

It holds the following tokens:

Object:

{
“user”: “johndoe”,
“admin”: false,
“uid”: 1000,
“groups”: [“users”, “wheel”, “audio”, “video”]
}

Strings: “user”: “johndoe” (keys and some values)

Value: 1000

Array : [“users”, “wheel”, “audio”, “video”]

References –

https://github.com/zserge/jsmn and https://www.json.org/

Follow Lynxbee
Tags:

Leave a Reply / Ask Question