top of page
Writer's pictureThe Tech Platform

YAML (Yet Another Markup Language): How It Works? Which is Better - YAML or JSON

Updated: Jul 28, 2023


YAML (Yet Another Markup Language)

YAML (Yet Another Markup Language) is a data serialization language that is often used for writing configuration files. Depending on whom you ask, YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.


Designed for human interaction, YAML is a strict superset of JSON, another data serialization language. But because it’s a strict superset, it can do everything that JSON can and more. One major difference is that newlines and indentation actually mean something in YAML, as opposed to JSON, which uses brackets and braces.


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

  • Strings

  • Numbers

  • Boolean

  • Dates and timestamps

  • Sequences

  • Nested values

  • Null values


YAML 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 “&”.


Example

Here's an example of a simple YAML file for an employee record that demonstrates the syntax rules.

--- 
# An employee record 
name: Martin D'vloper 
job: Developer 
skill: Elite 
employed: True 
foods:   
    - Apple   
    - Orange   
    - Strawberry   
    - Mango 
languages:   
    perl: Elite   
    python: Elite   
    pascal: Lame 
education: |   
    4 GCSEs   
    3 A-Levels   
    BSc in the Internet of Things


How To Write YAML

The basic structure of a YAML file is a map. You might call this a dictionary, hash, or object, depending on your programming language or mood.


Very generally, its keys and values all the way down:

key: value

How YAML Works

Full documentation for YAML can be found on its official site, but outlined below are some simple concepts that are important to understand when starting to use YAML.

  • Scalars, or variables, are defined using a colon and a space.

integer: 25
string: "25"
float: 25.0
boolean: Yes
  • Associative arrays and lists can be defined using a conventional block format or an inline format that is similar to JSON

--- # Shopping List in Block Format 
- milk 
- eggs 
- juice  
--- # Shopping List in Inline Format 
[milk, eggs, juice]

  • Strings can be denoted with a | character, which preserves newlines, or a > character, which folds newlines.

data: |    
    Each of these    
    Newlines    
    Will be broken up 

data: >    
    This text is    
    wrapped and will    
    be formed into    
    a single paragraph


Features of YAML (Yet Another Markup Language)


1. Delimiter collision resistance

YAML relies on indentation for structure, making it resistant to delimiter collision. Some languages require escape characters or sequences, padded quotation marks, and other workarounds for handling special characters. YAML is naturally insensitive to quotation marks and braces, making special characters easier to define, particularly in strings.


2. Security

In and of itself, YAML has no executable commands. It is simply a data-representation language. However, its integration with other languages allows Perl parsers, for example, which can execute Perl code. PyYAML, a parser, and emitter for Python, includes documentation specifically warning against this security vulnerability and has a built-in function to protect against dangerous Python objects known as yaml.safe_load.


Advantages of YAML

  • In YAML, there is no extra delimiter is used. So it is the lightweight than XML and JSON.

  • YAML, not using a delimiter also makes the reading light and simple. (I am not sure if you agree with this point. Because many geeks find it easier to read the data in content delimiter or tags separate those.)

  • YAML makes data understanding easy. So it is useful in the case of configuration.

  • YAML has a wide variety of uses and is popular. It can be used for configuration, then from transferring data to storing intermediate data.

  • YAML, not using a delimiter also makes the reading light and simple. (I am not sure if you agree with this point. Because many geeks find it easier to read the data in content delimiter or tags separate those.)both JSON and YAML code with a single parser (YAML parser).


Disadvantages of YAML

  • There are so many applications that are already built using XML or JSON, so it is hard for a developer to replace this with YAML. For instance, I am developing a plugin for an existing project which uses XML. I would like to use XML in the plugin. So it will be easy for me to merge the plugin with an existing project.

  • Talking about popularity, XML has a much more mature ecosystem than YAML.

  • JSON [JavaScript Object Notation] exists since the early 2000s and it is highly adopted by many as compared to YAML. So it is easier to find the support for JSON over YAML.

  • There are extra precautions while writing the YAML code. Even if you mismatch a single space while indentation, your code can stop working.

  • There are many ways to represent data in YAML and make a data hierarchy. So it is complex for processing. So JSON and XML have better performance than YAML.



Difference Between YAML and JSON

YAML (Yet Another Markup Language)

JSON

YAML

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

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

Supports simple hierarchy through associative arrays and lists.

Natively supports object references and relational trees.

Simple data types and structure.

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

Faster generation and parsing.

More complex data structures slow down parsing.

Active, engaged community, many available libraries.

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

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

More interesting for complex data serialization.

Which is Better?

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 (Yet Another Markup Language), 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 Tech Platform

Recent Posts

See All

コメント


bottom of page