Any person with an interest in programming and technology knows what JSON is. YAML is not as common as JSON, but it is also a popular and awesome data serialization language. For example, any person who has used Docker surely knows what YAML is.
First of all, let’s see what is meant by data serialization. According to Devopedia, data serialization is the process of converting data objects present in complex data structures into a byte stream for storage, transfer, and distribution purposes on physical devices. So YAML and JSON both are a way of storing the data objects and data structures in files.
What Is YAML?
YAML is the short name for YAML Ain’t Markup Language. And YAML Ain’t Markup Language is the short name for YAML Ain’t Markup Language Ain’t Markup Language. Pretty cool, right? To make things more interesting, the official YAML website is also displayed in YAML format.
YAML uses three dashes (
---) to indicate the start of a document and three dots (
...) to indicate the end of a document. Unlike JSON, YAML uses the indentations just like in Python to show the levels in the data. The key/value pairs are separated with a colon and the lists begin with a hyphen in YAML. And also YAML files are written with the extension YML in some places and both .YAML and .YML mean the same file type.
What Is JSON?
JSON has a standard format for data storing. It stores data in key/value pairs. The records are separated by commas and both field names and strings are enclosed with double quotes.
Pros and Cons: YAML vs. JSON
Theoretically, both YAML and JSON are expected to perform the same task, that is, providing a human-readable data interchange format.
Readability and Complexity
The design goal of JSON is to be as simple as possible and be universally usable. This has reduced the readability of the data, to some extent. In contrast, the design goal of YAML is to provide a good human-readable format and provide support for serializing arbitrary native data structures. This has increased the readability of the YAML files, but it has made the parsing and generation of files somewhat complex. We can see this clearly in the YAML official website where it shows the content in YAML format: It is easily readable for anyone visiting the site. On the other hand, if it was displayed in JSON format, the webpage would be useless.
It is said that YAML is a superset of JSON format. What is simply meant by this is that we can parse JSON using a YAML parser. However, in practical scenarios, this parsing may raise problems, but it is theoretically possible.
In the data serialization contest, JSON is the winner because of the ability to quickly and easily parse JSON serialized data with its simpler design. And this has made JSON more popular among developers which results in more and more native support and this has improved the performance once again. Therefore JSON has become the most widely used data interchange format for web applications and web services.
For any programming language, we can easily find a JSON library integrated with the language due to its popularity, its ease of implementation and its simplicity. The JSON official website lists many languages with many supporting libraries for JSON. YAML also has widespread support and many libraries for integrating it with various languages, but not as much as JSON. You can get the list of libraries and programming languages that support YAML here.
So far we discussed the pros of JSON over YAML. But some considerably important features of YAML are not found with JSON. YAML supports comments where JSON does not. We can comment anywhere in the document with a simple # character. This has proven advantageous when writing configuration files where one developer can easily describe the configuration using the comments. Therefore YAML format is used in many technology stacks like ElasticSearch, and Docker for storing configuration information.
Ability to Use Complex Structures
Another feature that YAML provides is the ability to reference other data objects. With this referencing, it is possible to write recursive data in the YAML file.
For this, we can define anchors in the YAML file using
& and refer to them later using aliases,
*. This is a very important feature in YAML that JSON does not offer. In JSON, it is impossible to serialize complex structures with object references. But the above feature in YAML solves that problem. But one drawback of this is the possibility of infinite looping in some converters.
So from all these points, we can see that both JSON and YAML have their strengths and their weaknesses. A good developer should be able to identify these and use the correct format in the correct place.
Frequently Asked Questions
What is the difference between YAML and JSON?
YAML and JSON are both data serialization languages, but they have a few differences when it comes to capabilities and syntax.
- Often used in creating configuration files for computer systems.
- Supports multiple data types and complex data structures.
- Syntax and format similar to written human language.
- Uses indentation to represent objects (similarly to Python).
- Often used for data exchange between systems and web applications.
- Supports number, string, Boolean, null, object and array data types.
- Uses curly brackets to represent objects.
Why is YAML so popular?
YAML is a popular data serialization language among developers because of its readability, versatility and use of an indentation system similar to that of Python's. It can handle a wide variety of data serialization tasks due to its support of multiple data types and programming languages.