0

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).

w3Schools says

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 XMLHttpRequest JSON is Unlike XML Because JSON doesn't use end tag JSON is shorter JSON is quicker to read and write JSON can use arrays

The 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.Parse the 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?

  • The question this is marked as a duplicate of partially answers the question, but does not answer all of the question asked. – Minecraftchest1 Mar 11 '21 at 15:35
  • The question and the duplicate are both off-topic for being opinion-based. I took the time to cover the core of the comparison as objectively as possible prior to closing the duplicate, but both questions are essentially off-topic on this site. – kjhughes Mar 11 '21 at 15:38
  • Do you have a suggestion of where I could move it to? – Minecraftchest1 Mar 11 '21 at 16:01
  • I cannot speak for all possible forums, but be aware that this is a very old topic that's already been covered from many perspectives. I doubt revisiting it specifically for your recent interest would generate anything new or interesting, especially since you seem to be starting so broadly in your inquiry and have nothing more particular to ask or add. – kjhughes Mar 11 '21 at 16:33

0 Answers0