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.
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.
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.
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.