Falcon80 Logo
JSON Rules

Representing structured data in JSOn is simple. Let's learn some ground rules first.

Rule 1:

A JSON data object always begins with curly braces and ends with curly braces.
{}

Rule 2:

All data is represented in the form of

"string":value

where the value can be any of the following:

* number
* string
* null
* boolean
* object
* array

Rule 3:

The rules for forming the strings (on the left hand side in red) are similar to rules for variables in most programming languages.
* They can be alphanumeric, but should always begin with a letter.
* They are case sensitive
* They can not contain special characters except underscores.

The following example shows valid strings.
{
"FirstName1":"Anoushka",
"Last_Name":"Nycil",
"Age":3
}

Rule 4:

String Values:

Strings on the right hand side can be made up of any unicode character EXCEPT the following:

* Double quotes " 
* Back slash \
* Control characters:  
    Quotation Mark: \"
    Reverse Solidus: \\
    Solidus: \/
    Backspace: \b
    Formfeed: \f
    Newline:  \n
    Carriage Return: \r
    Horizontal Tab: \t
    \u four digit hex number

So for example, the following string:value pairs are invalid.

{
"FirstName":" "Len" ";
"LastName":"\bistro";
}

Number Values:

Numbers can be integers, decimals, or exponentials. Exponentiation is represented by e, e+, e-, E, E+, E-

In the following example, all of the string:value pairs are valid.
{
"Length":55;
"breath":25.28;
"Area":22e+2;
}

Rule 5:

When there are multiple string:value pairs, they are comma seperated. So for example, if you want to represent a list of first names, this is how you do it using rule 1 and rule 2.

{
"firstname1":"Robert",
"firstname2":"Solomon",
"firstname3":"Jane",
"firstname4":"Veronica",
"firstname5":"Christopher"
}

Notice that the strings on the left hand side (in red) are always inside quotes. On the right hand side, you have values (in green), also within quotes. This is because the values are of type 'string'. You don't have to quote the values if they are of a different data type, as in the following examples.

{
"Name":"Isabelle",
"ID": 43231,
"Enrolled":true,
"classes":null
}

In the above example, the left hand sides are all still enclosed within quotes. On the right hand side, only the value for Name is enclosed within quotes because it is a string. The values for ID, Enrolled, and classes are not quoted because they are of type number, boolean and NULL respectively.

You don't always have to use double quotes for strings. If you are getting the JSON parsed in javascript, you can replace the double quotes with single quotes and it will still work. In fact, you don't even have to quote the strings on the left hand side and javascript will still manage to parse it correctly. So the following data structure is still valid.

{
"FirstName":"Dolly",
'LastName':'Nycil',
Class:"History"
}

However, using double quotes for strings on both sides of the colon is recommended as best practice.

Rule 6:

As mentioned already, one of the data types can be objects. Objects are wrapped in curly braces. So when the value of a string is an object, we are actually creating a nested JSON object.

{
"Student": { "Name":isabelle,
                "ID":43231,
                "Enrolled":{"History":true,
                               "Science":false
                              }
             }
}

In the above example, student is an object, with members Name, ID and Enrolled. Enrolled is another object with members History and Science.

Rule 7:

When representing array values, the values are enclosed within square braces [ value 1, value 2 ... Value n ]. See the following example.

{
"Students":[{"Name":"Isabelle","ID":43231,"Enrolled":true},
              {"Name":"Tonya","ID":43232,"Enrolled":true},
              {"Name":"Vivian","ID":43233,"Enrolled":true}]
}


Here, students is an array with 3 values. Each value is an object with members Name, ID and Enrolled.