What are the specific circumstances in which you used JSON over XML?

I still haven't used JSON in a real-world project - I almost always rely on XML.

+4
source share
8 answers

I prefer JSON whenever I have a choice. This is more self-documenting than XML (in the absence of a schema), because it distinguishes between:

  • records (objects, sub-elements, order is not significant) and
  • lists (arrays, numbered subclauses, order is significant).

In plain-formed XML, these two things are often represented as elements. Naively, we might think of using XML subelements for list items and XML attributes for named properties. But then the named property values ​​can only be strings, and not arbitrary nested structures. And the element name of subitems in lists is often redundant.

XML is just not well suited to its most common application (representing object hierarchies), and JSON is better suited.

This is not surprising, since XML is based on SGML, which was originally designed to apply markup to a plain text stream - a completely different application.

+7
source

Every time you need to move a lot of data with an XHR call.

We started translating all of our XHR calls to JSON because it is at least 4 times smaller than our old XML format.

Now that browsers like Firefox 3.5 have built-in JSON support, it's much easier to use.

+3
source

In short, JSON is a decent interop option, particularly for javascript. If that's enough, then what is the point of XML?

For more complex data, json is less beneficial in my mind. XML may be less "efficient", but I rarely came across this problem - but force naming of elements in xml means that it is often easy to debug - this is especially true for projects spanning multiple organizations - B2B interop, say. XML has the best equipment in the form of XSLT, XSD, integration into many languages ​​(native XML VB.NET, say LINQ to XML), and it is more naturally β€œopen” - it is easier to expand XML with a new element without breaking the old scheme, whereas the list in json contains unnamed elements, which makes functional collisions more likely. Of course, you can get around all these things in JSON, but this requires forethought and planning. Finally, although most languages ​​have decent XML support, javascript in particular does not, creating XML is painful if you do not use several helper functions (the DOM does not get any design prizes here).

Shortly speaking; I use XML for data exchange and probably longer-term complex APIs, and json specifically to facilitate working with a client server where a strict API is not needed, and therefore xml imperfect OO compliance and a bit heavier weight transfer is drag and drop does not match any of the benefits. If you intend to perform a small AJAX update, JSON works fine and avoids the OO / XML impedance mismatch (and less serialization means icing on the cake).

I have never worked on a large enough application where the JSON bandwidth savings had an impact, but if you are Google or Microsoft, I am sure there is also a significant benefit (and you probably need to spend a ton of planning and forethought, as any API that you release will quickly become addicted.)

+3
source
  • Easy
  • Simple conversion from strings to javascript objects by calling eval (), but be careful. See this question for more information on eval ().
  • You do not need to parse XML (this may mean increased performance in some situations).

But you will lose some of the great compatibility that XML offers.

+2
source

For me, JSON is simply easier to use, less overhead, easier to manipulate for me, my selected dojo + PHP tools, it is much easier to take data from the server and integrate it into the user interface.

+1
source

Always. For all the projects that I am currently involved in, I use JSON.

http://www.techyouruniverse.com/software/json-vs-xml-vs-serialize-for-data is a pretty solid resource why (and not my site).

This is a great, easy and fast way to integrate php with javascript. It also makes a fantastic way to store preferences for site users in a database.

+1
source

JSON is a much simpler data format. Even in well-defined XML, such as XML using schema, as is used in many SOAP web services, it is difficult to interact with data types. Even simple integers and logical values ​​can cause interaction problems, and more complex structures, such as nested arrays with additional values, are very difficult to do work in different type systems. JSON is a very simple system of a well-defined type and seems to be easier to interact with.

Another major drawback of XML is that the complexity of parsing leads to security issues. Simple XML is simple. But most XML libraries parse full XML, including PI and DTD and entity extensions. It is easy to misuse these XML libraries, resulting in XXE vulnerabilities and the like. JSON can also be used insecurely if you simply use () JSON data as code. But there are many easy-to-use JSON parsing libraries that reliably process JSON data as data, not code.

+1
source
  • Significantly lower overhead
  • Easier to read
  • Automatically generated in Firebug with the header "application / json"
+1
source

All Articles