0% found this document useful (0 votes)
56 views4 pages

JSON - Introduction

The document introduces JSON (JavaScript Object Notation) as a syntax for storing and exchanging data that is easier to use than XML. It provides an example of a JSON object defining an "employees" array with three records, and compares it to similar XML code. Key points covered include that JSON is language-independent, self-describing, and can be easily converted to native JavaScript objects.

Uploaded by

Jenila Vincent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views4 pages

JSON - Introduction

The document introduces JSON (JavaScript Object Notation) as a syntax for storing and exchanging data that is easier to use than XML. It provides an example of a JSON object defining an "employees" array with three records, and compares it to similar XML code. Key points covered include that JSON is language-independent, self-describing, and can be easily converted to native JavaScript objects.

Uploaded by

Jenila Vincent
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

JSON - Introduction

❮ Previous Next ❯

JSON: JavaScript Object Notation.

JSON is a syntax for storing and exchanging data.

JSON is an easier-to-use alternative to XML.

The following JSON example defines an employees object, with an array of 3


employee records:

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.

JSON - Evaluates to JavaScript Objects


The JSON format is syntactically identical to the code for creating JavaScript
objects.

Because of this similarity, instead of using a parser (like XML does), a


JavaScript program can use standard JavaScript functions to convert JSON data
into native JavaScript objects.

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>

<h2>JSON Object Creation in JavaScript</h2>

<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 »

Much Like XML Because


 Both JSON and XML are "self describing" (human readable)
 Both JSON and XML are hierarchical (values within values)
 Both JSON and XML can be parsed and used by lots of programming
languages
 Both JSON and XML can be fetched with an XMLHttpRequest

Much Unlike XML Because


 JSON doesn't use end tag
 JSON is shorter
 JSON is quicker to read and write
 JSON can use arrays

The biggest difference is:

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

 Fetch an XML document


 Use the XML DOM to loop through the document
 Extract values and store in variables

Using JSON

 Fetch a JSON string


 JSON.Parse the JSON string

❮ Previous Next ❯

You might also like