May 08, 2021 JSON
The JSON format supports the following data types:
Type | Describe |
---|---|
Digital (Number) | Double floating-point format in JavaScript |
String type | Double quote-wrapped Unicode characters and backslash escape characters |
Boolean | true or false |
Array | An ordered sequence of values |
Value | It can be strings, numbers, true or false, null, and so on |
Object | Out-of-order keys: Value pairs collection |
Whitespace | Can be used between any pair of symbols |
Null | Empty |
The following table shows the types of numbers:
Type | Describe |
---|---|
Shaping (Integer) | Numbers 1-9, 0 and plus or minus |
Score (Fraction) | Scores, such as .3, .9 |
Index (Exponent) | Indices, e.g. e, e, e-, E, E, E- |
Grammar:
var json-object-name = { string : number_value, .......}
Example:
The following example shows a numeric type whose value should not be wrapped in quotation marks:
var obj = {marks: 97}
The following table shows the string types:
Type | Describe |
---|---|
" | Double quotes |
\ | Backslash |
/ | Slash |
B | The back-checker |
F | Page break |
N | Line breaks |
R | Carriage return |
T | Horizontal tabs |
u | Four heteens |
Grammar:
var json-object-name = { string : "string value", .......}
Example:
The following example shows the string data type:
var obj = {name: 'Amit'}
It contains both true and false values.
Grammar:
var json-object-name = { string : true/false, .......}
Example:
var obj = {name: 'Amit', marks: 97, distinction: true}
Grammar:
[ value, .......]
Example:
The following example shows an array of multiple objects:
{
"books": [
{ "language":"Java" , "edition":"second" },
{ "language":"C++" , "lastName":"fifth" },
{ "language":"C" , "lastName":"third" }
]
}
Grammar:
{ string : value, .......}
Example:
The following example shows the object:
{
"id": "011A",
"language": "JAVA",
"price": 500,
}
You can insert between any pair of symbols. Y ou can add to make your code more readable. The following example shows a declaration that uses spaces and does not:
Grammar:
{string:" ",....}
Example:
var i= " sachin";
var j = " saurav"
Means empty type.
Grammar:
null
Example:
var i = null;
if(i==1) {
document.write("<h1>value is 1</h1>");
} else {
document.write("<h1>value is null</h1>");
}
Including:
Grammar:
String | Number | Object | Array | TRUE | FALSE | NULL
Example:
var i =1;
var j = "sachin";
var k = null;