- More secure
- Faster
- Stores a larger amount of data
- Stored data is not sent with every server request
Note: Local storage is per domain. All pages from one domain can store and access the same data.
JSON
stands for JavaScript Object Notation
JSON
is a way of communicating data with specific rules
JSON
is a syntax for storing and exchanging data
JSON
is text, written with JavaScript object notation
The syntax is taken from JavaScript
but JSON
is portable with other languages
JSON
is a lightweight data-interchange format
It's easy for humans to read and write
It's easy for machines to parse and generate
A common use of JSON is to exchange data to/from
a web server
application/json
is the official Internet media type for JSON
.json
is the JSON
filename extension{}
hold objects
Square brackets []
hold arrays
Data is represented in key/value
pairs
Each key
is followed by colon :
key/value
pairs are separated by comma ,
Array values are separated by comma ,
...
Basic structure of JSON
{
"book": [
{
"id": "01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt"
},
{
"id": "07",
"language": "C++",
"edition": "second",
"author": "E.Balagurusamy"
}
]
}
string
- double-quoted Unicode{ "name":"John" }
float
- precision floating-point number{ "age":30.5 }
integer
- number without a fractional component{ "age":30 }
boolean
- true or false{ "sale":true }
array
- an ordered sequence of values{ "employees":[ "John", "Anna", "Peter" ] }
object
- an unordered collection of key:value
pairs{ "employee":{ "name":"John", "age":30, "city":"New York" } }
null
{ "middlename":null }
,
These are enclosed in square brackets []
which means that array begins with [
and ends with ]
{
"books": [
{ "language":"Java" , "edition":"second" },
{ "language":"C++" , "lastName":"fifth" },
{ "language":"C" , "lastName":"third" }
]
}
key/value
pairs
Each key is followed by colon :
key/value
pairs are separated by comma
The keys must be strings and should be different from each other
Objects should be used when the key
names are arbitrary strings
Objects are enclosed in curly braces {}
, it starts with {
and ends with }
{
"id": "011A",
"language": "JAVA",
"price": 500,
}
- a function
- a date
- a symbol
- undefined
- Both JSON and XML are "self describing" (human readable)
- Both JSON and XML are hierarchical (values within values)
- Both JSON and XML are language independent
- Both JSON and XML can be parsed and used by lots of programming languages
- Both JSON and XML can be fetched with an XMLHttpRequest
JSON is Unlike XML Because+ JSON doesn't use end tag
+ JSON is shorter
+ JSON is quicker to read and write
+ JSON can use arrays
+ Better Performance
+ JSON can be parsed easily
+ JSON object has a type
+ All major JavaScript frameworks support JSON
+ Supported by all major JavaScript frameworks
+ Supported by most backend technologies
+ JSON is recognized natively by JavaScript (as object)
- JSON doesn't support comments
- JSON can not use attributes
- JSON offers poor extensibility as no namespace support
- Supports limited data types
JSON and XML examples
JSON (size: 52 bytes){
"company": Volkswagen,
"name": "Vento",
"price": 800000
}
XML (size: 79 bytes)<car>
<company>Volkswagen</company>
<name>Vento</name>
<price>800000</price>
</car>
Another example
JSON{
"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]
}
XML<employees>
<employee>
<firstName>John</firstName> <lastName>Doe</lastName>
</employee>
<employee>
<firstName>Anna</firstName> <lastName>Smith</lastName>
</employee>
<employee>
<firstName>Peter</firstName> <lastName>Jones</lastName>
</employee>
</employees>
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
// Returns the JSON representation of a value (pretty printed)
echo json_encode($arr, JSON_PRETTY_PRINT);
Result of the above code{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5
}
The following example shows how the PHP objects can be converted into JSONclass Emp {
public $name = "";
public $hobbies = [];
public $birthdate = "";
}
$e = new Emp();
$e->name = "sachin";
$e->hobbies = ["sport", "reading"];
$e->birthdate = date('Y-m-d', strtotime('2015-11-18'));
echo json_encode($e);
The above code will produce the following result{"name":"sachin","hobbies":["sport","reading"],"birthdate":"2015-11-18"}
Decoding JSON in PHP (json_decode)
The following example shows how PHP can be used to decode JSON objects$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
// Decoding into an object
var_dump(json_decode($json));
// Decoding into an array
var_dump(json_decode($json, true));
While executing, it will produce the following resultobject(stdClass)#1 (5) {
["a"]=> int(1)
["b"]=> int(2)
["c"]=> int(3)
["d"]=> int(4)
["e"]=> int(5)
}
array(5) {
["a"]=> int(1)
["b"]=> int(2)
["c"]=> int(3)
["d"]=> int(4)
["e"]=> int(5)
}
JSON.parse()
Parses the data and converts it into a JavaScript objectlet txt = '{"name":"John", "age":30, "city":"New York"}'
// Converts JSON string into a JavaScript object.
let obj = JSON.parse(txt);
// Puts the data into "p" element with id: demo
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age;