I recently heard about JavaScript Object Notation (JSON), and after looking it up, it seems like it's becoming rather popular as an alternative to the Extensible Markup Language (XML).
I went on this page for more info, but it seemed more of an XML-bashing page rather than a comparison page. So I thought I should ask here:
What are the benefits of JSON as compared to XML, and why (if at all) should we choose one over the other?开发者_开发技巧
- JSON is more compact and can be easily loaded in JavaScript.
- XML is stricter and has support for schemas and namespaces.
On the face of it JSON seems superior in every way - it's flexible, more compact and in many cases easier to use (especially when working with JavaScript), however it lacks some key features, in particular:
- Schema support,
I.e. the ability for party A to specify the format of a document, and the ability for party B to check that they are supplying something that matches this format.
This is crucial when passing data between separate systems, where a deviation from the expected format might mean that the data cannot be processed (or worse, is processed incorrectly).
- Namespace support,
I.e. the ability to mix data intended to be read by multiple sources (or written by multiple sources) in the same document.
An example of this in action is the SOAP protocol - namespaces allow for the separation of the SOAP "Envelope", or "Wrapper" data which is passed alongside the serialised application data. This allows web frameworks process and handle the SOAP Envelope and then pass the body / payload data onto the application.
JSON is very useful when developing a web application where fast, compact and convenient serialisation of data is required, however it's flexible nature is the very thing that makes it less suitable than XML for transferring data between separate systems, or storing data that will be read by 3rd parties.
Perhaps in time these sorts of features will appear in JSON, but for now XML is the dominant format for things like web services and file formats.
Advantages of JSON
- Smaller message size
- More structural information in the document
- Can easily distinguish between the number
1
and the string"1"
as numbers, strings (and Booleans) are represented differently in JSON. - Can easily distinguish between single items and collections of size one (using JSON arrays).
- Can easily distinguish between the number
- Easier to represent a null value
- Easily consumed by JavaScript
Advantages of XML
- Namespaces allow for sharing of standard structures
- Better representation for inheritance
- Standard ways of expressing the structure of the document: XML schema, DTD, etc
- Parsing standards: DOM, SAX, StAX
- Standards for querying: XQuery and XPath
- Standards for transforming a document: XSLT
Draw
- Human Readable
- Easy to parse
XML
- Can have a schema that states its format.
- This is of interest to quality control people. You can prove that its format matches what is expected, and therefore you may not have to be quite as fervent as you might otherwise be at checking that a field exists within it every time you want to reference one.
- (Though this pre-supposes that you go out of your way to actually validate the XML against its schema.)
- Bloated; each field name has to be written out twice per field. Ew!
JSON
- Far less bloated, easier to parse and arguably more human readable (if you space it out properly).
- Not quite as powerful: not expressive enough to separate attributes from values.
Advantages of XML
Near ubiquitous support in a wide array of languages and frameworks. More likely than not there's already a tool out there to help your extract information from an XML response.
It can adhere to a concrete schema if so you choose. Once it validates, you can say it's correct and start parsing.
Namespaces allow you to divide the XML.
Advantages of JSON
Lightweight in comparison to XML. Fewer characters = smaller time going through the
internet tubes
.Easier to handle with Javascript if you need something for a web application.
JSON - smaller and can be natively loaded as JavaScript object (speed is a value)
XML - still standard, however older (slower, bigger, but not only JS)
JSON is a much simpler format than XML. JSON is intended only for describing data as an object hierarchy, while XML has many other features as well.
If you only need to send plain simple data, then JSON is a good alternative, as that is just what it's intended for.
If you need a more powerful way of describing your data then you would need XML, as JSON simply doesn't do anything fancy.
They're equally expressive formats. JSON is less verbose. JSON can be parsed easily in JavaScript (and other languages), and XML is parsed easily by many things too.
I think the most important consideration is what will be consuming the data - if you already have a bunch of XML-interpreting code, stick with that. If all your clients are web developers, go with JSON.
Less verbose- XML uses more words than necessary
JSON is faster- Parsing XML software is slow and cumbersome. Many of these DOM manipulation libraries can lead to your applications using large amounts of memory due to the verbosity and cost of parsing large XML files.
JSON data model’s structure matches the data: JSON’s data structure is a map whereas XML is a tree. Although a map (just key/value pairs) can be limiting, that’s what we want, because it is easier to interpret and is predictable.
In code: Items are represented the same way in code. In many languages, especially dynamic ones, you can just ‘slurp in the JSON’ and you immediately have your domain object. It is easy to go from objects in JSON to the objects in code because they align. When going from objects in XML to objects in code they do not align and there is a lot of room for interpretation.
JSON is limiting, but that’s a good thing: JSON is limited in terms of what objects can be modeled. Some may think XML is better because more objects can be modeled and it doesn’t prohibit developers. But even though JSON prohibits developers, it is in a positive way, making the code simpler, more predictable, and easy to read. XML can be formatted to look and function any way a company wants, but it makes it difficult for developers to read, understand, and convert. In most cases people believe XML is better because developers can do anything under the sun but in the age of simplifying, less is more, making JSON a better alternative.
精彩评论