1.1 Data Structures supported by JSON

JSON supports two widely used data structures.

  • A collection of name/value pairs

Different programming languages support this data structure but refer to it by different names like: object, record, struct, dictionary, hash table, keyed list, or associative array.

  • An ordered list of values

In various programming languages, this is called an array, vector, list, or sequence.

Since data structures supported by JSON are also supported by most of the modern programming languages, JSON is a very useful data-interchange format.

1.2 Object Structure

{ string : value, .......}

Explanation of Syntax

An object starts and ends with { and }. Between them, a number of string value pairs can reside. String and value is separated by a : and if there are more than one string value pairs, they are separated by ,.

Example

{
"firstName": "Bidhan",
"lastName": "Chatterjee",
"age": 40,
"email":"bidhan@example.com"
}

In JSON, objects can nest arrays within it. Arrays start and end with [ and ] . The following example shows that.

Example

{
"Students": [
{ "Name":"Amit Goenka" ,
"Major":"Physics" }, 
{ "Name":"Smita Pallod" ,
"Major":"Chemistry" }, 
{ "Name":"Rajeev Sen" , 
"Major":"Mathematics" }
]
}

1.3 Array Structure

Array Syntax

[ value, .......]

Explanation of Syntax

An Array starts and ends with [ and ]. Between them, a number of values can reside. If there are more than one values residing, they are separated by ,.

Example

[100, 200, 300, 400]

If the JSON data describes an array, and each element of that array is an object it would look like this.

[
{
"name": "Bidhan Chatterjee",
"email": "bidhan@example.com"
},
{
"name": "Rameshwar Ghosh",
"email": "datasoftonline@example.com"
}
]

Remember that even arrays can also be nested within an object. The following shows this.

{
"firstName": "Bidhan",
"lastName": "Chatterjee",
"age": 40,
"address":
{
"streetAddress": "144 J B Hazra Road",
"city": "Burdwan",
"state": "Paschimbanga",
"postalCode": "713102"
},
"phoneNumber":
[
{
"type": "personal",
"number": "09832209761"
},
{
"type": "fax",
"number": "91-342-2567692"
}
]
}

1.4 Value Data Types

Appart from the Object or the Array, a value can be a string, a number, an object, an Array, a Boolean value (i.e. true or false) or Null.

  • String
  • Number
  • Boolean
  • NULL

String

A string is a sequence of zero or more Unicode characters, enclosed by double quotes, using backslash escapes. A character is represented as a single character string, similar to a C or Java string.

The following table shows supported string types.

String Types Description
" A double quotation mark.
\ Reverse Solidus
/ Solidus
b Backspace
f form feed
n newline
r Carriage return
t Horizontal tab
u Four hexadecimal digits

Number

The following table shows supported number types.

Number Types Description
Integer Positive or negative Digits.1-9. And 0.
Fraction Fractions like .8.
Exponent e, e+, e-, E, E+, E-

Whitespace

Whitespace can be placed between any pair of supported data-types.

1.4.1 Using Javascript and JSON Tools

It's a good idea to have your JSON checked for validity. You can use an online tool or one from your editor.

You can check any Javascript including Javascript Objects or JSON at JS Hint. It is very opinionated but you can adjust how picky it is.

JS Hint

Or you can use JSONLint. This is really a JSON validator.

JSONLint

Notice that you can also put a URL. So if you're trying to check the validity of a PHP script that generates JSON, then you can just paste the URL and it will check that as well.

Or you can use JSON Editor Online. You can see it right here.

JSON Editor Online

You can also use it to minify your JSON. This is actually an excellent tool for checking out your schema, the structure of your JSON documents, as you're designing them.

A good off-line tool is Cocoa JSON Editor.