DEV Community

noeticforce
noeticforce

Posted on • Originally published at noeticforce.com

YAML vs JSON: Know the Difference Between the Two

YAML and JSON both deal in data and are used to interchange data between systems and humans. YAML is a strict superset of JSON, which essentially means that all JSON files are also valid YAML files but all YAML files are not valid JSON files.

From a practical usage perspective, JSON is majorly used for interchanging data between web applications over the internet via APIs. YAML on the other hand is mainly used for maintaining configuration files and storing data for offline processing. 

Being a superset of JSON, YAML offers much more capabilities, but on the contrary, most developers are aware of and use JSON whereas YAML is not so popular.

Read on to know why.

The two key reasons these formats are heavily used in application development – 

  • Human readability – You can open, easily understand, and modify the contents of both YAML and JSON files in any text editor. Many popular text editors like vim, emacs, and VS code provide first-class support for YAML and JSON file editing.

  • Ease of parsing by computer programs – Almost every popular programming language has ready-to-use programs/libraries to parse the content of JSON and YAML. You can also programmatically generate JSON or YAML files from arrays, maps, text files, and database tables.

In this article, we are taking the YAML vs JSON debate forward, you will also get guidance on which one to use where.

Let us start with a brief introduction of the two, followed by the use cases they serve, key differences, popularity, ease of use, and more. 

What is YAML?

YAML first appeared back in 2001 and is used mainly to create, edit and maintain application configuration files. Technically speaking, YAML files are text files that support Unicode characters and are written using YAML standard specifications

Though YAML files are majorly generated programmatically, developers often edit and maintain configuration files manually, using text editors. YAML specifications were originally proposed by Clark Evans by taking bits and pieces from many other programming languages like C, HTML, Perl, and MIME.

Some of the specifications taken from other languages include whitespace wrapping which is similar to HTML, escape sequences based on C language, document separator “—-“ picked from MIME and arrays from Perl. 

Given below is an example of a simple YAML file –

 

---
 react: a javascript UI library
 github-stars: 89000
 dev-cost: 100.50
 day-shift: true
 dev-names:
   - bill
   - steve
   - elon
   - jeff
 project-details:
   dev-count: 4
   total-cost: 500
   iterations: 5
   phases:
     count: 6
     reviews: "twice a week"
   customer: noeticforce
Enter fullscreen mode Exit fullscreen mode

In the above example, you can see that the YAML document starts with three dashes (—). Then you see key-value pairs of different data types, followed by a list collection, and then a nested object.

Also, notice that double quotes around strings are not mandatory, the use of white spaces is the key in YAML. We will cover more about the capabilities of YAML in the following sections.

What is JSON?

JSON Logo

JSON (JavaScript Object Notation) is a lightweight text format that follows javascript object syntax. Although JSON was originally derived from JavaScript, it is used as a language-independent standard format. Almost all programming languages utilize arrays and key-value pairs in one way or the other, and that is where JSON fits in perfectly. 

The data in the JSON file is organized either as an ordered list of values or a collection of name-value pairs, both nested to the level necessary. 

The dominance of javascript on the web and JSON being javascript native makes JSON the data exchange format of choice among web developers. Many web and API developers rely completely on JSON and wouldn’t even know about the existence of the YAML format. 

Given below is an example of a JSON file-

{
  "react": "a javascript UI library",
  "github-stars": 89000,
  "dev-cost": 100.5,
  "day-shift": true,
  "dev-names": [
    "bill",
    "steve",
    "elon",
    "jeff"
  ],
  "project-details": {
    "dev-count": 4,
    "total-cost": 500,
    "iterations": 5,
    "phases": {
      "count": 6,
      "reviews": "twice a week"
    },
    "customer": "noeticforce"
  }
}
Enter fullscreen mode Exit fullscreen mode

Notice the use of double quotes, brackets, commas, and colons in the JSON example above, these are more or less mandatory as per JSON specifications.

YAML vs JSON: Key Differences

While you can do everything in YAML that JSON is capable of doing but you wouldn’t necessarily use YAML for all use cases, and there are multiple reasons for that. 

Let us look at some of the key differences between YAML and JSON to understand this better-

Parsers and Interoperability

JSON specifications are simple, and for that reason, you will find multiple ready-to-use parsers for JSON in almost all programming languages. On the other hand, YAML specifications are vast, and implementing a full parser for YAML in any language is difficult and requires more effort. 

Having said the above, YAML also has ready-to-use parsers in many languages, but not all implement the full specifications of YAML. This makes YAML much less interoperable as compared to JSON. 

It is worth noting that developers typically struggle to validate complex YAML files with the capabilities of available parsers and generators. 

You may also like to read: Best React Development Tool

Readability

In YAML, you are allowed to define simple data structures by using just space indentations and without using punctuation like quotes, parentheses, brackets, colons, and commas. This allows you to create highly readable configuration files that are easily interpretable by business users.

You can also use YAML capabilities to create and store highly complex data structures interpretable only by computer programs, but that is not what most developers do with YAML. 

JSON files on the other hand lie somewhere in between. While developers can easily interpret the JSON files, business users typically find it difficult to interpret complex and nested JSON documents.

Data Processing Performance: JSON vs YAML

Due to the simple specifications of JSON, one needs smaller programs to serialize and deserialize data in JSON. On the other hand, YAML generators and interpreters have to cover a vast set of specifications and hence are comparatively large.

The above also helps in faster processing and lesser memory consumption to process the same amount of data in JSON as compared to YAML.

Having said the above, you can still write highly efficient YAML generators and parsers that utilize only part of the YAML specifications. But then, if the use case is simple, why not use JSON itself.

Comments Support: YAML or JSON

The ability to add comments is one of the key features that system admins and developers miss in JSON. While in YAML, it is very easy to add comments. One can simply add a # character to mark the content as a comment in the YAML document.

As the configuration files become complex, describing each config option becomes important. This is the reason YAML has replaced JSON wherever human-readable configuration files are needed. Git for example uses YAML to store repository and archival configuration details with comments. Docker also uses YAML for almost all of the system configurations. 

Ok, but do we need comments when systems talk to each other, not really! This is why JSON still rules the realm of web services and system integrations. Check out any open API available on the web and you will find JSON as an option for the data delivery format.

Complex Data Structures

Handling SQL database like complex data structures using text files is a difficult proposition. But if your use case requires you to do that, YAML would fit in but not JSON. 

In YAML, you can add anchors and refer to the anchors using aliases from anywhere in the file. The option to add references to other objects makes YAML a good option to serialize interlinked and complex data objects. 

YAML vs JSON: Quick Comparison

YAML JSON
YAML: YAML Ain’t Markup Language JSON: JavaScript Object Notation
Used mainly to store and maintain app configurations Used mainly to exchange data between systems via Webservices/APIs
You can use # to add comments in YAML files You can not add comments in JSON files
The key objective is to create human-readable files with the ability to exchange data between systems The key goal is to exchange data between systems while still maintaining human readability
Too many specifications, takes time to master Simple specifications, easy to learn
Supports strings, numbers, arrays, objects, null values, booleans, date/time, nested values, sequences, references, and aliases. Mainly used for strings, numbers, objects, and arrays.
It is quite complex to create a full parser and generator for YAML Easy to create parsers and generators for JSON
Memory heavy, relatively slow Lightweight parsers, fast data processing

Closing Thoughts

Hope you got the info you were looking for around YAML vs JSON comparison. Given below is a quick note on when to use YAML and where JSON fits better.

JSON is simple, widely used, and is a perfect option for sending and receiving data around the web. Use it to connect multiple systems, build client-server data exchange mechanisms, and make data available to the world as open APIs.

YAML is adopted by system admins and application developers for configurations. You can use it for something as simple as storing the true/false option in the configuration file or as complex as structured relational objects. 

You might also like:

\

Top comments (0)