When to prefer JSON over XML ?
XML over JSON if any of these is true:
- Need message validation
- We using XSLT
- Messages include a lot of marked-up text
- We need to interoperate with environments that did not support JSON
JSON over XML if all of these are true:
- Messages no need to be validated, or validating their deserialization is simple
- We are not transforming messages, or transforming their deserialization is simple
- The messages are mostly data, not marked-up text
- The messaging endpoints have good JSON tools
The alternate answer:
Use JSON unless using XML. It is simpler to understand, because it requires less configuration and it is easier to program for reading and writing if the libraries are available in the context, and they are ubiquitous now.
For instance,When Amazon first exposed their catalogs as a web service, they offered both JSON and XML. Something like 90% of the implementers chose JSON.
Preferring JSON over XML:
- At the same time evaluating JSON on the client-side will be more efficient and faster.
- JSON serialization produces shorter strings than XML. Using JSON will reduce the amount of data running across the wire and improve performance in that respect.