JSON - Introduction
JSON - Introduction
❮ Previous Next ❯
JSON Example
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
The following XML example also defines an employees object with 3 employee
records:
XML Example
<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>
What is JSON?
JSON stands for JavaScript Object Notation
JSON is a lightweight data-interchange format
JSON is language independent *
JSON is "self-describing" and easy to understand
* JSON uses JavaScript syntax, but the JSON format is text only, just like XML.
Text can be read and used as a data format by any programming language.
Try it Yourself
With our editor, you can edit JavaScript code online and click on a button to
view the result:
JSON Example
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var text = '{"name":"John Johnson","street":"Oslo West
16","phone":"555 1234567"}';
var obj = JSON.parse(text);
document.getElementById("demo").innerHTML =
obj.name + "<br>" +
obj.street + "<br>" +
obj.phone;
</script>
</body>
</html>
Try it Yourself »
XML has to be parsed with an XML parser. JSON can be parsed by a standard
JavaScript function.
Why JSON?
For AJAX applications, JSON is faster and easier than XML:
Using XML
Using JSON
❮ Previous Next ❯