JSON vs XML
Both JSON and XML are used to receive data from a web server.
JSON and XML differ in notation, as shown below:
JSON example
{
"
sites
"
:
[
{
"
name
"
:
"
Runoob Tutorial
"
,
"
url
"
:
"
www.runoob.com
"
}
,
{
"
name
"
:
"
"
,
"
url
"
:
"
www.google.com
"
}
,
{
"
name
"
:
"
"
,
"
url
"
:
"
www.weibo.com
"
}
]
}
XML example
<
sites
<
site
<
nameRunoob Tutorial
name
<
url
www.runoob.com
url
site
<
site
<
name
google
name
<
url
www.google.com
url
site
<
site
<
name
Weibo
name
<
url
www.weibo.com
url
site
sitesSimilarities between JSON and XML:
- Both JSON and XML data are "self-describing" and easy to understand.
- Both JSON and XML data have a hierarchical structure
- Both JSON and XML data can be used by most programming languages
Differences between JSON and XML:
- JSON does not need end tags
- JSON is more concise
- JSON is faster to read and write
- JSON can use arrays
The biggest difference isXML needs an XML parser to be parsed, while JSON can be parsed using standard JavaScript functions.
Why is JSON better than XML?
XML is harder to parse than JSON.
JSON can be parsed directly using existing JavaScript objects.
For AJAX applications, JSON loads faster and is simpler than XML data:
Using XML
- Get the XML document
- Use the XML DOM to iterate through the document
- Extract the data and copy it to variables
Using JSON
- Get the JSON string
- JSON.parse parses the JSON string
Related Articles
- JavaScript JSON: https://www.runoob.com/js/js-json.html
- XML DOM tutorial: https://www.runoob.com/dom/dom-tutorial.html