What is JSON?

JSON or JavaScript Object Notation is a format that implements unstructured text representation of the structured data, based on the principle of pairs key-value and the ordered lists. Although JSON began its spread with JavaScript, it’s supported in most languages, either natively, or using special libraries. Usually Json is used to exchange information between web clients and the web server.

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:

{
“firstName”: “Jonathan”,
“lastName”: “Freeman”,
“loginCount”: 4,
“isWriter”: true,
“worksWith”: [“Spantree Technology Group”, “InfoWorld”],
“pets”: [
{
“name”: “Lilly”,
“type”: “Raccoon”
}
]
}

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.

JSON is a common data format with the minimum number of value types – strings, numbers, Boolean values (one or zero), lists, objects and zero. Despite the fact that JSON is a subset of JavaScript, such data types is available in most popular programming languages which makes JSON a good candidate to transfer data between programs written in different languages.

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.

At that time, the transmission charging was carried out for the amount of data transferred, so the developers knew that a restart of the whole page is very expensive and considered new technologies to improve the user interface. Then you can create a web query in the background that was added in Internet Explorer 5 turned out to be quite a viable approach to phase data is loaded for display. Instead of reloading the page, clicking the button simply perform a web query that will be run in the background. The content will be updated as soon as loaded. It can be controlled with JavaScript, a versatile programming language for browsers.

Initially the data was transmitted in XML format, but it was difficult to use it in JavaScript. In JavaScript the objects have already been used to represent data in the language, so Douglas Crockford took the JS object syntax and used it as a specification of a new data interchange format called JSON. This format was much easier to read and parse in the browser using JavaScript. Soon developers began to use JSON instead of XML.

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:

<?xml version="1.0"?>
<person>
<first_name>Jonathan</first_name>
<last_name>Freeman</last_name>
<login_count>4</login_count>
<is_writer>true</is_writer>
<works_with_entities>
<works_with>Spantree Technology Group</works_with>
<works_with>InfoWorld</works_with>
</works_with_entities>
<pets>
<pet>
<name>Lilly</name>
<type>Raccoon</type>
</pet>
</pets>
</person>

In addition to the redundancy code, in fact, a data record has taken two times more space, XML introduces a certain ambiguity in the analysis of the data structure. Convert XML to JavaScript object can take from tens to hundreds of lines of code and requires fine tuning for each of the analyzed object. Convert JSON into a JavaScript object running in one line and does not require any prior knowledge about the analyzed object.

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.

Insights

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.

Source: www.infoworld.com

Source: losst.ru

(Visited 25 times, 1 visits today)