JSON Data Types

JSON values must be one of the following data types.

- a string
- a number
- an object (JSON object)
- an array
- a boolean
- null

JSON Strings
Strings in JSON must be written in double quotes.

{ "name":"David" }

Value can be any-Unicode-character except " or \ or control-character.

If we want a quotation in the string value, we should add backslash.

{ "description": "He lives in \"India\"." }

The following backlash escapes can be used when needed.

\" - Quotation mark
\\ - reverse solidus
\/ - solidus
\b - backspace
\f - formfeed
\n - newline
\r - carriage return
\t - horizontal tab
\u - 4 hexdecimal digits

JSON Numbers
Numbers in JSON must be an integer or a floating point. The values must be an integer(ex: digits 1-9, 0 and +ve or -ve), fraction (ex: .3, .9) or exponent(ex: e, e+, e-, E, E+, E)

{ "age":30 }

JSON Objects
Values in JSON can be objects. It is an unordered set of name/value pairs. Objects are enclosed in curly braces. It starts with '{' and ends with '}'. Each name is followed by ':'(colon) and the key/value pairs are separated by , (comma). The keys must be strings and unique value. Which means we cannot use the same key more than once.

Objects should be used when the key names are arbitrary strings.

"employee":{ "name": "David", "age": 32, "city": "California" }

Objects as values in JSON must follow the same rules as JSON objects.

JSON Arrays
Values in JSON can be arrays. It is an ordered collection of values which are enclosed in square brackets which means array begins with [ and ends with ]. The values are separated by , (comma). Array indexing can be started at 0 or 1.

Arrays should be used when the key names are sequential integers.

"employees":[ "David", "Clara", "Martin" ]

JSON Booleans
Values in JSON can be true/false.

{ "isHoliday": true }

JSON null
Values in JSON can be null.

{ "middlename": null }
Related Tutorial