JSON - DataTypes

JSON format supports the following data types −
Number
double- precision floating-point format in JavaScript
String
double-quoted Unicode with backslash escaping
Boolean
true or false
Array
an ordered sequence of values
Value
it can be a string, a number, true or false, null etc
Object
an unordered collection of key:value pairs
Whitespace
can be used between any pair of tokens
null
empty
It is a double precision floating-point format in JavaScript and it depends on implementation.
It is a double precision floating-point format in JavaScript and it depends on implementation.
Octal and hexadecimal formats are not used.
Octal and hexadecimal formats are not used.
No NaN or Infinity is used in Number.
No NaN or Infinity is used in Number.
The following table shows the number types −
Integer
Digits 1-9, 0 and positive or negative
Fraction
Fractions like .3, .9
Exponent
Exponent like e, e+, e-, E, E+, E-
var json-object-name = { string : number_value, .......}

Example showing Number Datatype, value should not be quoted −
var obj = {marks: 97}
It is a sequence of zero or more double quoted Unicode characters with backslash escaping.
It is a sequence of zero or more double quoted Unicode characters with backslash escaping.
Character is a single character string i.e. a string with length 1.
Character is a single character string i.e. a string with length 1.
The table shows various special characters that you can use in strings of a JSON document −
"
double quotation
\
backslash
/
forward slash
b
backspace
f
form feed
n
new line
r
carriage return
t
horizontal tab
u
four hexadecimal digits
var json-object-name = { string : "string value", .......}

Example showing String Datatype −
var obj = {name: 'Amit'}
It includes true or false values.
var json-object-name = { string : true/false, .......}

var obj = {name: 'Amit', marks: 97, distinction: true}
It is an ordered collection of values.
It is an ordered collection of values.
These are enclosed in square brackets which means that array begins with .[. and ends with .]..
These are enclosed in square brackets which means that array begins with .[. and ends with .]..
The values are separated by , (comma).
The values are separated by , (comma).
Array indexing can be started at 0 or 1.
Array indexing can be started at 0 or 1.
Arrays should be used when the key names are sequential integers.
Arrays should be used when the key names are sequential integers.
[ value, .......]

Example showing array containing multiple objects −
{
   "books": [
      { "language":"Java" , "edition":"second" },
      { "language":"C++" , "lastName":"fifth" },
      { "language":"C" , "lastName":"third" }
   ]
}
It is an unordered set of name/value pairs.
It is an unordered set of name/value pairs.
Objects are enclosed in curly braces that is, it starts with '{' and ends with '}'.
Objects are enclosed in curly braces that is, it starts with '{' and ends with '}'.
Each name is followed by ':'(colon) and the key/value pairs are separated by , (comma).
Each name is followed by ':'(colon) and the key/value pairs are separated by , (comma).
The keys must be strings and should be different from each other.
The keys must be strings and should be different from each other.
Objects should be used when the key names are arbitrary strings.
Objects should be used when the key names are arbitrary strings.
{ string : value, .......}

Example showing Object −
{
   "id": "011A",
   "language": "JAVA",
   "price": 500,
}
It can be inserted between any pair of tokens. It can be added to make a code more readable. Example shows declaration with and without whitespace −
{string:" ",....}

var obj1 = {"name": "Sachin Tendulkar"}
var obj2 = {"name": "SauravGanguly"}
It means empty type.
null

var i = null;

if(i == 1) {
   document.write("<h1>value is 1</h1>");
} else {
   document.write("<h1>value is null</h1>");
}
It includes −
number (integer or floating point)
string
boolean
array
object
null
String | Number | Object | Array | TRUE | FALSE | NULL

var i = 1;
var j = "sachin";
var k = null;

    20 Lectures 
     1 hours 

 Laurence Svekis

    16 Lectures 
     1 hours 

 Laurence Svekis

    10 Lectures 
     1 hours 

 Laurence Svekis

    23 Lectures 
     2.5 hours 

 Laurence Svekis

    9 Lectures 
     48 mins

 Nilay Mehta

    18 Lectures 
     2.5 hours 

 Stone River ELearning
 Print
 Add Notes
 Bookmark this page