Over the past 15 years JSON has become a formal data exchange standard and is used almost everywhere on the Internet. Today it is used by almost all web servers. Such popularity has led to the fact that many databases support JSON. Modern relational databases such as PostgreSQL and MySQL now support the storage and export of data in JSON. Databases such as MongoDB and Neo4j also supports JSON, although MongoDB uses a slightly modified version of JSON. In this article we will discuss what is JSON better than XML, its disadvantages, and when it is better to use.
What is JSON?
To understand why JSON is and how it works can not do without practice. First, let us consider the following example:
“worksWith”: [“Spantree Technology Group”, “InfoWorld”],
In this structure we have clearly identified some of the attributes of a person. First, we determined the name, the number of authorizations in the system, whether this person is a writer, a list of the companies with which it works and the list of Pets. This or similar structure may be sent from the server to the web browser or a mobile app that can already do everything you need with these data, for example to display them or to keep.
Why should you use JSON?
To understand the usefulness and importance of JSON, we need to know a bit about the history of interactivity in the Internet. In early 2000, the interactivity of web sites has started to change. At that time the browser is only used to display information and all the work on the preparation of the content display carried out the web server. When the user clicks the button in the browser, the request sent to the server, which collected and sent the HTML page, ready to display. Such a mechanism was slow and inefficient. This required that the browser re-redraws everything on the page even if you change a small part of the data.
Now fast data exchange with JSON is the de facto standard for data transfer between server and client, mobile apps and even the internal system services.
JSON vs XML
As I said above, the main alternative to JSON is XML. However, XML is becoming less common in new systems. And very easy to understand why. The following is an example data record that you saw above in the Json over XML:
<works_with>Spantree Technology Group</works_with>
The limitations of JSON
Although JSON is relatively compressed and flexible data format that is easy to work on many programming languages, it does have some drawbacks. Here are some restrictions:
- There is no structure. On the one hand, this means that you have full flexibility to present the data in any way. On the other, you can easily store unstructured data.
- Only one type of numbers. supported floating-point format and double-precision IEEE-754. It is quite a lot, but you can’t use the variety of numeric types that are in other languages.
- No date type. developers should use the string representation of dates, which can cause a mismatch formatting. Or to use a date-number of milliseconds since Unix epoch (1 January 1970).
- There is no review – you will not be able to do the annotation for fields that require this in your code.
- Detail – although JSON less verbose than XML, it is not the most compressed format for data exchange. For high-performance or specialized services you want to use more effective formats.
When should you use JSON?
If you develop software that interacts with a browser or native mobile application, you better use JSON. The use of XML is deprecated. For communication between servers, JSON may not be very effective and make better use of the serialization infrastructure similar to Apache Avro Apache Thrift or. Even here, JSON is not a bad choice, and it can give everything that you need. But there is no exact answer what to choose.
If you are using a MySQL database, your program will largely depend on what is being done in the database. In relational databases, which supports JSON is a good practice to use it as little as possible. A relational database was developed for data in a specific schema. Although most of them now support the JSON data format, performance it will be much lower.
JSON is a data format designed primarily for data transmission between web servers and browsers or mobile applications. The format has a simple design and flexibility, it is easy to read and understand, and easy to work with such data in most programming languages. The lack of a strict schema provides format flexibility, but such flexibility is sometimes difficult to read, and understand data.
You may have difficult when working with JSON to strongly typed languages such as Scala or Elm, but the wide spread of the format suggests that there are utilities and libraries that will help even for complex tasks. Now you know what is json and how to use it.