JSON
JSON
JavaScript Object Notation
JSON is a syntax is for storing and exchanging data.
JSON is a text, written with JavaScript Object Notation.
If you have data stored in a JavaScript object, you can convert the object into JSON, and send it to a server:
var myObj = { name: "John", age: 31, city: "New York" };
var myJSON = JSON.stringify(myObj);
window.location = "demo_json.php?x=" + myJSON;
If you receive data in JSON format, you can convert it into a JavaScript object:
var myJSON = '{"name":"John", "age":31, "city":"New York"}';
var myObj = JSON.parse(myJSON);
document.getElementById("demo").innerHTML = myObj.name;
var myObj = { name: "John", age: 31, city: "New York" };
var myJSON = JSON.stringify(myObj);
window.location = "demo_json.php?x=" + myJSON;
If you receive data in JSON format, you can convert it into a JavaScript object:
var myJSON = '{"name":"John", "age":31, "city":"New York"}';
var myObj = JSON.parse(myJSON);
document.getElementById("demo").innerHTML = myObj.name;
When storing data, the data has to be a certain format, and regardless of where you choose to store it, text is always one of the legal formats.
JSON makes it possible to store JavaScript objects as text.
var myObj, myJSON, text, obj;
// Storing data:
myObj = { name: "John", age: 31, city: "New York" };
myJSON = JSON.stringify(myObj);
localStorage.setItem("testJSON", myJSON);
// Retrieving data:
text = localStorage.getItem("testJSON");
obj = JSON.parse(text);
document.getElementById("demo").innerHTML = obj.name;
JSON Data types
strings
Strings in JSON must be written in double quotes.
{ "name":"Vino" }
Numbers
Numbers in JSON must be an integer or a floating point.
{ "age":30 }
Objects
Values in JSON can be objects.
{
"employee":{ "name":"John", "age":30, "city":"New York" }
}
"employee":{ "name":"John", "age":30, "city":"New York" }
}
Arrays
Values in JSON can be arrays.
{
"employees":[ "John", "Anna", "Peter" ]
}
"employees":[ "John", "Anna", "Peter" ]
}
Booleans
Values in JSON can be true/false.
{ "sale":true }
Null
Values in JSON can be null.
{ "middlename":null }
JSON Parse
When we receive data from server.
'{ "name":"John", "age":30, "city":"New York"}'
function JSON.parse() to convert text into a JavaScript object:
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');
Parsing functions
'{ "name":"John", "age":30, "city":"New York"}'
function JSON.parse() to convert text into a JavaScript object:
var obj = JSON.parse('{ "name":"John", "age":30, "city":"New York"}');
Parsing functions
Functions are not allowed in JSON.
If you need to include a function, write it as a string.
You can convert it back into a function later:
var text = '{ "name":"John", "age":"function () {return 30;}", "city":"New York"}';
var obj = JSON.parse(text);
obj.age = eval("(" + obj.age + ")");
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age();
var obj = JSON.parse(text);
obj.age = eval("(" + obj.age + ")");
document.getElementById("demo").innerHTML = obj.name + ", " + obj.age();
JSON Stringify
No comments