YAML vs. JSON: Breaking Down the Key Differences

In software development, YAML and JSON are often used for configuring applications, setting up infrastructure, and interacting with web application interfaces (Web APIs). While both can be used interchangeably in some cases, they are very different. This article discusses the features they bring to the table and their key differences.

What is JSON?

JavaScript Object Notation(JSON) is a data format that makes it easy for machines to parse and generate data. It’s widely used in web applications requiring data transfer. It supports different data types, such as numbers, strings, booleans, or objects. In addition, some programming languages like Javascript and Python have built-in support for JSON, which makes developing software using it for data transfer really convenient.

What is YAML?

YAML is a human-readable data-serialization language. It is a superset of JSON, which means YAML can contain JSON objects. In addition to supporting JSON data types, it also supports dates and timestamps. YAML is often used for configuration files in different programming languages and platforms.

Difference Between YAML vs. JSON

Now that you understand what YAML and JSON are, let's look at the key differences between the two.

Syntax

While YAML uses indentation to structure its fields and objects, JSON uses curly braces.

Below is an example of a YAML object defining a person:

id: 1
name: anonymous
address:
	city: Hanoi
	country: Vietnam

Below is the same object definition using JSON:

{
	"id": 1,
	"name": "anonymous",
	"address": {
		"city": "Hanoi",
		"country": "Vietnam"
	}
}

Using indentation to separate fields and objects makes YAML more readable than JSON.

Data Types

As for data types, JSON supports strings, objects, numbers, booleans, arrays, and null. Besides the data types that JSON supports, YAML provides additional support for dates and timestamps.

Below is a YAML example of an object with a timestamp value:

userId: 1
userName: anonymous
createdTime: 2023-05-17T00:00:00Z
updatedTime: 2023-05-17T00:00:00Z

Comments

In software development, comments are great for improving the collaboration experience for people working on the same piece of code. Unlike JSON, YAML supports comments, allowing you to explain the responsibility of a line or block of code. This is especially important for complicated YAML files, such as Kubernetes deployments and GitLab CI deployment files.

Below is an example of a YAML file containing comments:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
...
rules:
# EventListeners need to be able to fetch all namespaced resources
- apiGroups: ["triggers.tekton.dev"]
	resources: ["eventlisteners", "triggerbindings", "triggertemplates", "triggers", "interceptors"]
	verbs: ["get", "list", "watch"]
- apiGroups: [""]
# configmaps is needed for updating logging config
	resources: ["configmaps"]
	verbs: ["get", "list", "watch"]
# Permissions to create resources in associated TriggerTemplates
- apiGroups: ["tekton.dev"]
	resources: ["pipelineruns", "pipelineresources", "taskruns"]
	verbs: ["create"]
...

Since there are several permissions that the role needs access to, it's important to have the comments in place so that other people working on the same team can understand the meaning of these permissions. This makes YAML a great asset in a collaborative development environment.

Extensibility

In terms of extensibility, YAML supports object references and aliases, allowing you to reference the same object multiple times within a document. JSON does not have built-in support for references or aliases, making it less flexible when you want to reuse your code. Below is an example of YAML code that uses an alias:

definitions:
	steps:
	- step: &unit-integration-test
		name: Unit and Integration Test
		script:
			- pytest tests/unit
			- pytest tests/integration
pipelines:
	branches:
	develop:
		- step: *unit-integration-test
	main:
		- step: *unit-integration-test

In this deployment pipeline, the execution scripts for step “unit-integration-test” are only defined once. You can trigger the “unit-integration-test” step in different code branches without redefining the execution scripts by referencing the defined step using “*unit-integration-test”.

Programming Languages Built-In Support

Programming languages such as Javascript and Python have built-in support for JSON. This allows you to read and generate data in JSON format easily when working with these two languages. There is no programming language that has built-in support for YAML. This makes it less effective in application development.

Ease of Learning and Adoption

Although YAML is more readable, it requires a steeper learning curve than JSON because of several reasons:

  • YAML offers more functionalities and data types support than JSON
  • YAML is not natively supported in software programming languages, so you have to use a library or create a new one to work with YAML

In terms of adoption, YAML and JSON suit different use cases.

  • YAML is widely adopted for configuring infrastructure and applying infrastructure as code, thanks to its readability.
  • JSON, on the other hand, is often used in web API due to its great support for serialization data.

JSON vs. YAML: Which Do I Use?

Both JSON and YAML have their own pros and cons, as you have learned in the sections above. Depending on your specific needs, the answer to the question will be different.

Since JSON is great at serialization and some programming languages have built-in support for it, choose it if you need to:

  • Work with the web APIs
  • Store temporary data
  • Integrate with another system
  • Logging and data analytics

Because of a high number of features and design for readability, choose YAML if you need to:

  • Create configuration files
  • Apply CI/CD deployment pipelines
  • Apply infrastructure as code
  • Create human-readable data representation

FAQ

Why Does Kubernetes Use YAML Instead of JSON?

Kubernetes uses YAML over JSON because of several reasons:

  • YAML offers greater readability
  • YAML supports more expressive data structures, allowing you to create nested and complicated configurations
  • YAML also supports comments which help to improve the collaboration between members working on the same configuration files.

Why Is JSON Preferred in APIs?

Below are the reasons why JSON is more popular and preferred in APIs development:

  • It is light-weight and more compact than YAML, which allows for data stored in JSON format to be transmitted faster over HTTP request
  • It is natively supported in some programming languages like Javascript or Python and has a number of libraries to work with
  • It is compatible with web browsers since it was built to work with Javascript.

Conclusion

In summary, YAML provides better readability and is better suited for configuration in infrastructure and deployment pipelines. JSON, on the other hand, is better at serialization and better suited for storing data in web APIs.

To learn how to deploy applications using AWS service, check out the AWS CodePipeline course.

To learn more about how JSON works, check out the FREE JSON Path Test  Course.