From Development

YAML vs. JSON: What is the difference?

JSON and YAML are two popular formats for data exchange between different applications and languages, also known as data serialization.

They’re similar in function and features: they represent data objects and structures and use a simple syntax that facilitates readability and editability.

But differences in design affect their scope of use. This overview of YAML and JSON will show their main features and compare them to help you make the right choice for your project.

Table of contents

What is JSON
   ➤  The JSON History
   ➤  Tech specs
       ➤  Data types used by JSON
       ➤  Syntax
       ➤  JSON Example
   ➤  Advantages of JSON
   ➤  Disadvantages of JSON
What is YAML
   ➤  The YAML History
   ➤  Tech specs
       ➤  Data types
       ➤  Syntax
       ➤  YAML Example
   ➤  Advantages of YAML
   ➤  Disadvantages of YAML
Differences between JSON and YAML
YAML or JSON: Which is better?

What is JSON

JSON means JavaScript Object Notation. As you might suspect by the name, JSON derives from JavaScript data formats. It is an open standard, lightweight, text-based format with limited data types. Its short syntax and simple structure make it very easy to read.

It’s used to send data to the server and from server to client, like an envelope in which the data is enclosed and sent back and forward between correspondents.

It’s highly appreciated by programmers who use it in mobile apps, database services like MongoDB or Kubernetes, and user interactions, being a more user-friendly alternative to XML. The simplicity and speed of processing that JSON offers and its high compatibility with any system make it one of the most popular formats in use today.

The JSON History

According to JSON's developer, Douglas Crockford, a language using the same principles was already being used at Netscape in 1996, but JSON only became mainstream as an established syntax in 2001.

It is a go-to resource for Android technology, REST-API, or any situation where data is sent from a server to a web page. It is language-independent, which means it can be used with any programming language. And a huge part of our online experience relies on it.

Tech specs

What are the defining features of a JSON document? JSON file extensions are defined as .json. It uses universal data structures and a limited choice of data types. Its simple syntax uses conventions familiar to programmers of the C-family of languages, like C, C++, C#, Java, JavaScript, Perl, Python, etc. As the official JSON website explains:

“An object is an unordered set of name/value pairs. An object begins with {left brace and ends with }right brace. Each name is followed by :colon, and the name/value pairs are separated by ,comma.”

Data types used by JSON

  • Strings
  • Numbers
  • Object
  • Arrays
  • Boolean (true/false)
  • Null values

Syntax

JSON’s syntax rules are simple.

  • Root node must be an array or an object.
  • Data is designated as name/value pairs.
  • Data entries are separated by commas.
  • Objects are limited between curly braces.
  • Square brackets hold arrays.
  • Objects can have array objects to create lists.
  • It uses quotes around names and values, which are separated by a colon.

JSON Example

Let’s look at a JSON example, using some information about the famous heavy metal band Metallica.

{
  "bandName": "Metallica",
  "genre": "Heavy Metal",
  "active": "yes",
  "bandMembers": {
    "drums": "Lars Ulrich",
    "guitarLead": "Kirk Hammet",
    "guitarRythm": "James Hetfield",
    "bass": "Cliff Burton"
  },
  "otherMembers": ["Jason Newsted",
    "Robert Trujillo",
    "Dave Mustaine",
    "Ron McGovney"
  ],
  "Discography" : [
    {"name":"Kill ’Em All","year":"1983"},
    {"name":"Ride the Lightning","year":"“1984"},
    {"name":"Master of Puppets","year":"“1986"},
    {"name":"..And Justice for All","year":"“1988"}
  ]
}

As you can see, curly brackets delimit both the object and the arrays. All items are quoted, and the attribute name is separated from the value by a colon. A comma is used to separate name:value pairings.

Lists are defined by using square brackets, where the array attributes are also separated by commas.

Advantages of JSON

  • Its compact syntax makes JSON very human-readable.
  • Simple syntax with limited markup.
  • Fast parsing by systems and languages.
  • It can be used by most systems and languages.

Disadvantages of JSON

  • Limited data types: only accepts strings, numbers, JSON object, array, boolean, and null.
  • Doesn’t accept: namespace, comment, or attribute.
  • JSON’s structure is meant to be simple, so it may not support complex configurations on its own.

What is YAML

YAML used to mean Yet Another Markup Language, but that meaning changed to YAML Ain't Markup Language to express its data focus over a document focus. Acronyms aside, YAML is a very human-readable, lightweight format, commonly used to store configuration information for DevOps tools like ElasticSearch, Docker, Kubernetes, Prometheus, and Ansible.

Like it says on the official website, also written in YAML format:

“YAML is a human-friendly data serialization language for all programming languages.”

It is a data serialization language, but it is also often used for writing configuration files. We’ll be looking at YAML through the data serialization prism.

The YAML History

YAML was first publicized in 2001. The founding members of YAML are IngydotNet, Clark Evans, and Oren Ben-Kiki, who joined efforts to create a simpler format than XML. You can read the story from a firsthand perspective. Since then, it has become a workhorse for developers everywhere.

Tech specs

YAML is written in a very clear human-readable format, with the added advantage of supporting comments, making it super easy to edit. Since it is a superset of JSON, a valid YAML file can contain JSON objects. It is suited to support complex data types, with the ability to enclose multi-level objects. This makes it less friendly to use with some technologies.

Data types

YAML accepts the same data types as JSON, the main difference being the ability to support date attributes.

  • Strings
  • Numbers
  • Boolean
  • Dates and timestamps
  • Sequences
  • Nested values
  • Null values

Syntax

The absence of brackets and quotes for most of the functions makes the reading more natural. Even the uninitiated get a quick understanding of what is supposed to happen. YAML’s syntax is its strength but also a source for validation problems if we don’t pay attention to indents and space positions.

  • Uses key-value pairs, separated by colons.
  • Springs are not enclosed in brackets.
  • Indentations are used to define data hierarchy.
  • Lists begin with hyphens.
  • Allows comments by preceding them with a #.
  • Spaces before attributes are important.
  • Pipe Character (|) allows for multiline strings; the “greater than” sign (>) allows multiline strings to be read as a single line.
  • YAML can reference other data objects by using “&”.

YAML Example

Now, see how the same data we used above for JSON looks in YAML format:

bandName: Metallica
genre: Heavy Metal
active: 'yes'

bandMembers:
  drums: Lars Ulrich
  guitarLead: Kirk Hammet
  guitarRythm: James Hetfield
  bass: Cliff Burton
  
otherMembers:
  - Jason Newsted
  - Robert Trujillo
  - Dave Mustaine
  - Ron McGovney
  
Discography:
  - name: Kill ’Em All
    year: '1983'
  - name: Ride the Lightning
    year: “1984
  - name: Master of Puppets
    year: “1986
  - name: ..And Justice for All
    year: “1988

On a first impression, YAML looks better than JSON since it gets rid of all the brackets and quotes. It doesn’t read much like code but more as an outline.

Advantages of YAML

  • Very simple human-readable syntax.
  • Compact syntax, which uses Python-style indentation to denote structure.
  • Supports scalers such as int, binary, str, and bool, and collections such as map, set, pairs, and seq.
  • YAML supports comments where JSON does not. Just add a # symbol before the comment.
  • Ability to use complex data structures.
  • By using the “&” symbol, YAML can reuse blocks. This is one of the reasons it is used to describe infrastructure, like Kubernetes stacks. JSON doesn't have an equivalent operation.

Disadvantages of YAML

  • Incorrect indentation or spacing easily generates validation errors.
  • The declarative nature of YAML makes debugging more difficult: the Alpha-Code for Norway (NO) can be interpreted as a boolean value.
  • No breakpoints.

Differences between JSON and YAML

YAML and JSON are two popular languages, similar in structure and usability. Their differences in design, syntax, and functionality make the choice between them a matter of purpose.

 

  JSON YAML

Syntax

Easy to read, though with brackets, commas, and quotes.

Easier to read than JSON, but sometimes leading to misinterpretations in attribute values.

Hierarchy

Supports simple hierarchy through associative arrays and lists.

Natively supports object references and relational trees. 

Complexity

Simple data types and structure.

Possibility to have complex data structures with more complex data types available.

Performance

Faster generation and parsing.

More complex data structures slow down parsing.

Support 

Active, engaged community, many available libraries.

Smaller community than JSON, which means fewer libraries and support.

Functionality

It has fewer native features than YAML, which curbs data serialization complexity. 

More interesting for complex data serialization.

YAML or JSON: Which is better?

When choosing between JSON or YAML format, just follow function: what do you need them to do?

JSON has a faster delivery since it works perfectly as a simple data exchange format. But it is limited to the supported data types, which can require extra resources to deal with data sets with a larger number of data type options. It is easy to read once you’re over the brackets and quotes, which is an advantage since they delimit specific items, reducing the risk of parsing misinterpretation.

YAML, on the other hand, almost doesn’t read like code. And the matryoshka-like ability to include objects within objects allows for greater data complexity. The downside? Such complexity slows down the parsing and generation process. But with a range of complex data types available, it has its appeal and is a great option to expand the types of information to be made available.

The purpose and priority of the information to deliver are also important, and a logical hierarchy should be defined before starting coding. This prior organization will define the structure of the document and how data will be presented.

No matter the choice, it’s all about what your data is supposed to do in the end.

Found this article useful? You might like these ones too!

At Imaginary Cloud, we simplify complex systems, delivering interfaces that users love. If you’ve enjoyed this article, you will certainly enjoy our newsletter, which may be subscribed below. Take this chance to also check our latest work and, if there is any project that you think we can help with, feel free to reach us. We look forward to hearing from you!