Both XML and JSON are languages which are used for storing and transporting data and objects. Both are widely used, and there is wide support for both. What I am wondering is when should one use XML, and when should one use JSON? The only real differences that I see between the two is the format ( somedata vs {"sometag : "somedata"} and datamoreData versus {"sometag" : {"subTag" : "data","anotherSubTag" : "more data"}) and the architecture differences (XML doesn't differentiate between arrays and plain sub-tags or care about the difference between strings and ints).
JSON is 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
XMLHttpRequestJSON is Unlike XML Because JSON doesn't use end tag JSON is shorter JSON is quicker to read and write JSON can use arraysThe biggest difference is:
XML has to be parsed with an XML parser. JSON can be parsed by a standard JavaScript function.
Why JSON is Better Than XML
XML is much more difficult to parse than JSON. JSON is parsed into a ready-to-use JavaScript object.
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.Parsethe JSON string
I don't see what one can do in JSON that cant be done with XML or vice versa, all thing being equal. Assuming one wrote a program to read and write both JSON and XML the same way (read using parser functions and written using generator functions), is there and advantage besides size that would influence the use of one or the other?
When should one use one or the other?
Can the 2 be used interchangeably (besides the functions used to read and write it)?
EDIT:
The answer at JSON or XML? Which is better? says
Choose XML if
- An industry standard XSD exists.
- You value more mature validation standard and tools.
- You need to transform the data to another XML form. (XSLT is excellent for transformations.)
- Or, you have to represent mixed content (tags mixed within text).
Choose JSON if
- The closer fit to JavaScript is valuable to you or your callers.
- You prefer a lighter-weight solution.
- Or, the above Choose XML if reasons do not apply to you.
This gives me some reasoning on when to use one or the other. Are there any other reasons that might influence the choice?