JSON schema
is a JSON document that describes other JSON document. It is a specification for JSON based format to define the structure of JSON data- Describes your existing data format
- Defines the structure of a JSON message
- Clear, human- and machine-readable documentation
- Complete structural validation, useful for automated testing
- Complete structural validation, validating JSON message
- Can be used to validate API request and response
Object
- a collection of key/value pairs
Array
- an ordered list of valuesvar obj = { name: "John", age: 30, city: "New York" };
// Converts object into JSON string
var myJSON = JSON.stringify(obj);
// Puts the string into element with id: demo
document.getElementById("demo").innerHTML = myJSON;
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;
$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)
}
- 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>
- a function
- a date
- a symbol
- undefined
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,
}