JSON (JavaScript Object Notation) is the most widely used data format for data interchange on the Web.JSON is a lightweight text-based data interchange format that is completely language independent. It is based on a subset of the JavaScript programming language and is easy to understand and generate.
JSON supports six main data types.
- String
- Number
- Boolean
- null/empty
- Object
- Arrays
Note: string, number, boolean, null are simple data types or primitive data types, while objects and arrays are called complex data types.
String: JSON strings must be written in double quotes, as in C. There are various special characters (escape characters) in JSON that you can use in strings, such as \ (backslash), / (forward slash), b (backspace), n (newline), r (carriage return), t (horizontal tab), etc.
Example.
{ "name":"Vivek" }
{ "city":"Delhi\/India" }
here \/ is used for Escape Character / (forward slash).
Number: expressed in base 10, not in octal and hexadecimal format.
Example.
{ "age": 20 }
{ "percentage": 82.44}
Boolean: This data type can be true or false.
Example.
{ "result" : true }
Null: This is just a null value.
Example.
{
"result" : true,
"grade" :, //empty
"rollno" : 210
}
Object: It is a set of name or value pairs inserted between {} (brackets). The keys must be strings and should be unique, and multiple key and value pairs are separated by (commas).
Syntax.
{ key : value, .......}
Example.
{
"People":{ "name":"Peter", "age":20, "score": 50.05}
}
Array: It is an ordered collection of values starting with [ (left brackets) and ending with… ending (right parentheses) at the beginning. The values of the array are separated by (commas).
Syntax.
[ value, .......]
Example.
{
"people":[ "Sahil", "Vivek", "Rahul" ]
}
{
"collection" : [
{"id" : 101},
{"id" : 102},
{"id" : 103}
]
}