JSON vs YAML – An in-depth comparison

No Comments
Modified: 13.03.2022

Do you want to check how to create JSON and YAML files, while knowing the difference between each other? You will get an overview of the different structures and how u can execute them in the different file types.

Overview

  1. What are JSON and YAML?
  2. The different Structures
  3. Other

What are JSON and YAML?

The JavaScript Object Notation (JSON) and YAML are two file types that are mainly used for configuration, storage, and data transmission. Additionally, both are human-readable and thus are easily editable. One big advantage of using these filetypes is that most programming languages have a package to import and extract the data.

Both are pretty similar, but YAML has some additional features like comments and relational anchors.

For example, REST APIs use JSON to transmit data after receiving a request by a user of the API. The structure of the sent data is the JSON format. On the other hand, docker-compose uses YAML for the configuration file of the docker containers. If you are interested in docker-compose check out this post for a short overview of the different properties.

The different Structures

In this section we get an overview of the different structures and their syntactic execution in both JSON and YAML. The first code field contains the JSON and the second code field contains the YAML. In addition to the structures, we have a look on the different data types.

Value

The value is the most basic structure and you will use it most of the time. In this structure, the key defines a value. When imported and extracted you get the value by calling the key. The value can be every type and every structure.

Need help or want to share feedback? Join my discord community!

"key" : "value"
key: value

List

The List is a collection of values.

[
	"value1",
	"value2"
]
- value1
- value2

Dictionary

The Dictionary is a collection of key-value pairs and each key contains an accessible value.

KOFI Logo

If this guide is helpful to you and you like what I do, please support me with a coffee!

{
	"key1" : "value1",
	"key2" : "value2"
}
key1: value1
key2: value2

Nested Dictionary

You can also create dictionaries with a key. This creates a nested dictionary and with this, you can create an infinitely deep nested file.

{
	"key1" : {
		"key2" : "value"
	}
}
key1:
	key2: value

Data types

Now we will have a look at the different data types by using the simple value structure for examples. The key will contain the current data type and the value will contain an example for it.

String

JSON requires you to mark a string with quotation marks. YAML on the other hand will interpret it, but you can also mark it with quotation marks.

"string": "hello world"
string: hello world

Number

"number": 42.1
number: 42.1

Boolean

"boolean": true
boolean: true

Other

Newlines in YAML

include_newlines: |
            this will include
            all newlines

fold_newlines: >
    this will seperate it with
    a space

    but you can create a newline
    by leaving one empty line in between

I hope this comparison comes in handy for you at some point. If I missed something, you want to give some feedback, or just have a question feel free to leave a comment or send me an email at mail@programonaut.com.

Discussion (0)

Add Comment

Your email address will not be published. Required fields are marked *