JSON VS XML
There is another design question that always in the plate. Should we use json or xml as our data interchange pass through different system layers, APIs or system integration.
Let us talk about scenes to be used and different of these 2 most use of data format in the system data interchange.
1. Speed
JSON is obviously more lightweight. transfer/process speed is faster than XML. Especially in Browser/Server architecture application pass through different layers and APIs.
So generally use it will be used for data interchange in different layers.
For example, JSON Data in REST API and also frontend javascript component can pass JSON data to backends layers or APIs.
2. Readability
JSON is coming from Javascript. So if you use JSON as data storage, its readability will not be good as XML. Especially when your data model get more complicated and hierarchical elements present in your data model, JSON will not be a good fit.
XML has better readability as it is coming from the structural language definition.
3. Data Storage
In some cases, it will be better to storage data into files compare to store in DB.
If you want to store the lightweight configuration data, JSON should be a better choice.
In other cases. You should always choose XML as your preference.
For example, Some of the E-learning application and projects, According to SCORM standard, Most of study related content are stored in XML format e.g., Topic, Step etc. Then loaded into your learning applications.
4. Syntax strictness
XML data strictness is better than JSON. XML can define the schema, this will help you to validate your data model as very baseline feature.
领英推è
XML can use parser to detect your XML data format if fit specification or not.
If JSON want do this, you need to write custom code or use other configuration do it.
In this case, XML is more used in the backends of the application to do data interchange compare to the frontend of the application
5. Application scenarios
XML seems more complicated than JSON as the way it presents in file.
But this is originally from its strictness. When your data model is complicated and also need some native validation. XML should be the better choice in this case. For example, DTD validation in XML.
Document data storage and the backends of the system configuration data storage are the most use case scenarios to use XML.
XML used in more wide range of backends open source framework as configuration data format.
For example, Spring Framework - applicationContext.xml
Mybatis - mapper.xml
Hibernate - hibernate.cfg.xml
Maven - pom.xml
You will see why these example that do not use JSON for a reason.
XML has another powerful feature. It is namespace. This is what JSON does not have.
JSON is more used in REST API data transfer with frontend javascript and also in some flat configuration data.