I heard someone state that "JSON is the same as XML." Are JSON and XML comparable? What are the major similarities and differences of each?
There are a few StackOverflow Q&As which touch on comparing JSON and XML [1] [2] [3] [4] [5] [6] [7], but none provide a good, single point of reference focused on their major similarities and differences.
XML is a document markup language, providing semantics that gives additional meaning to data; JSON does not have this ability. XML has a broader range of specifications that cover such things as schemas for data definition and validation, namespaces, stylesheets, transformations, data expressions and many others.
JSON object has a type whereas XML data is typeless. JSON does not provide namespace support while XML provides namespaces support. JSON has no display capabilities whereas XML offers the capability to display data. JSON is less secured whereas XML is more secure compared to JSON.
JSON (JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange
. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers
.
Borrowing a JSON sample from Wikipedia, a JSON representation of a person might look like:
{
"firstName" : "John",
"lastName" : "Smith",
"address" :
{
"street": "21 2nd Street",
"city" : "New York",
"state" : "NY",
"zip" : "10021"
},
"phoneNumber":
[
{
"type" : "home",
"number": "212 555-1234"
},
{
"type" : "fax",
"number": "646 555-4567"
}
]
}
XML (eXtensible Markup Language) is a simple, very flexible text format derived from SGML
. According to Wikipedia, XML is a set of rules for encoding documents in machine-readable form. [... It's goals] emphasize simplicity, generality, and usability over the Internet. It is a textual data format[...].
Again borrowing a XML sample from Wikipedia, an XML representation of a person might look like:
<person firstName="John" lastName="Smith">
<address street="21 2nd Street" city="New York" state="NY" zip="10021" />
<phoneNumber type="home" number="212 555-1234" />
<phoneNumber type="fax" number="646 555-4567" />
</person>
Based on these simple definitions, it's understandable that one could conclude that JSON and XML are comparable. If your goal is to use one or the other for simple data interchange over the Internet, as exhibited by the simple examples above, that goal is surely attainable and JSON and XML are, indeed, mostly comparable.
However, as you dig deeper into the specifications of each you begin to realize they are completely different standards with similar goals; each with overlapping use cases where one is much better suited than the other and where they are equivalent solutions (where the choice is a matter of the specific use case).
Though it may be just a bit biased, The Fat-Free Alternative to XML
from the JSON site lists some good points of comparison. There are also the XML vs JSON !!! Dont compare.
and JSON Pros and Cons
blog entries with some bullet points for comparison.
Here is a set of analogous relationships which can be used to convert from XML to JSON:
Here is a set of analogous relationships which can be used to convert from JSON to XML:
References
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With