Data Structures & Algorithms

YAML vs JSON – Which is better?

Nowadays, almost every person is familiar with the standard format of JSON. Contrarily, individuals who use Docker are surely familiar with YAML. In simpler words, Docker is a toolkit which permits developers to run, build, deploy, modify as well as stop packages through a single API or commands. YAML is a new but popular language used to serialize data. First of all, we should perceive what data serialization is. Data serialization is the most common way of transforming data objects into byte streams used to store, transfer and distribute data on devices. However, they have similar objectives to store structures and data objects into files but distinctive ways to work.

In this article, we first go through the features of JSON and YAML, then compare them in-depth to completely comprehend their advantages, and then briefly discuss which one is better.

YAML

YAML is an abbreviation of Yet Another Markup Language and is used to define configurations. It is lightweight and represents data in a human-readable format. If you want to parse JSON then you have to use the YAML parser as JSON is a subset of YAML. Moreover, JSON can be converted into YAML. YAML also has JSON in its configuration files. YAML not only permits user-defined data types but also allows explicit data typing. “.YML” or “.YAML” are extensions of YAML.  Here is the link to its official documentation https://yaml.org/. YAML differs from JSON as it uses Python-like indentations to represent levels in data. In YAML, lists start with hyphens and key pairs can be separated with a colon. Three dashes (“—”) indicate the beginning of a document whereas three dots (“…”) indicate the end of a document.

Example

Below is the example to show the YAML format. This example contains information about various cars.

---
List of Cars:
-  car registration number: 011
   car name:  Civic
   owner of car: Zaim Khan
   model number: xyz
   price: 20 lacs
   color: black
-  car registration number: 012
   car name:  Mehran
   owner of car: Sara Khan
   model number: xyzasd21
   price: 10 lacs
   color: white
-  car registration number: 013
   car name:  Cultus
   owner of car: Taimur Ali
   model number: xy12z
   price: 15 lacs
   color: grey
...

JSON

First of all, we quickly define what JSON is. JSON depends on the JavaScript language. JSON can be used with any programming language as it is not language independent but mostly used with JavaScript. It stores data in a standard format. In JSON, records can be separated through commas whereas strings and fields are enclosed within double quotes (“ ”).

Example

Below is the example to show the JSON format. This example stores information related to cars.

{

"List of Cars" : [
{
"car registration number":"011",
"car name":"Civic",
"owner of car":"Zaim Khan",
"model number":"xyz",
"price":"20 lacs",
"color":"black"
},
{
"car registration number":"012",
"car name":"Mehran",
"owner of car":"Sara Khan",
"model number":"xyzasd21",
"price":"10 lacs",
"color":"white"
},
{
"car registration number":"013",
"car name":"Cultus",
"owner of car":"Taimur Ali",
"model number":"xy12z",
"price":"15 lacs",
"color":"grey"
}
]
}

YAML VS JSON:

YAML and JSON are not as simple to compare as you think. If we talk about the readability of data from configuration files then both JSON and YAML can be used interchangeably. Therefore, the comparison is quite difficult in terms of readability. In the configuration file, JSON might be able to express the same data types just like YAML. Although YAML is a set of key/value pairs, there is no object in it.

Moreover, JSON is a full-fledged data structure used extensively for storing data in programming languages like JavaScript. On the other hand, YAML is not present outside the configuration file.

JSON is best suited in terms of serialization format whereas YAML is better as a configuration. JSON has a serialization format that has originated from JavaScript objects

In JSON, strings can be used in double-quotes whereas YAML supports both single and double-quotes. Comments are not allowed in JSON while comments are represented through hash or number signs in YAML.

Conclusion

It is quite difficult to decide whether JSON  is better or YAML. Therefore, I have decided to compare these with different aspects. When you talk about readability, YAML is much better, but moving towards the size then JSON takes the edge. JSON is comparatively faster than YAML. However, if data configurations are small then YAML is better since its interface is much more friendly.

JSON has a feature to encode six different data types like an object, array, strings, numbers, null and boolean. JSON is much easier for machines to parse and it is quite flexible compared to YAML which allows encoding of python data but with vulnerabilities while decoding. That’s why YAML is too hard to parse.

About the author

Shehroz Azam

A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.