What’s the Difference Between YAML and JSON?

YAML and JSON are two data serialization formats that provide a human-readable data exchange mechanism. They format data in a standardized manner for exchange between software applications, and their text is also readable by humans. Both represent data as key-value pairs. JSON supports data objects as values, while YAML does not. However, YAML supports more data types and is closer in its use of natural language to support developer use.

Read about JSON »

Data representation: YAML vs. JSON

The appearance and syntax of JSON and YAML are similar but slightly different.

At a glance, JSON format appears more machine-like in its data representation than YAML. A JSON message includes these symbols:

  • Curly brackets for objects
  • Colons to separate key-value pairs
  • Square brackets for arrays 
  • Quotation marks to enclose strings

JSON was built from a small subset of JavaScript. So, its syntax makes JSON documents look similar to programming code.
 

Unlike JSON, the YAML format looks more like how someone would write down structured data on paper. It was designed specifically to be easier for humans to read. It uses the following:

  • Indentation to represent objects 
  • Colons to separate key-value pairs
  • Hyphens for arrays
  • Hashes to denote a comment

YAML was originally built to simplify XML.

Document examples: YAML vs. JSON

Here are examples of how a document containing a shirt’s product data would look like in YAML and JSON.

YAML

JSON

type: T-Shirt

price: 20.00

sizes:

  • S
  • M
  • L

   reviews: # Note about reviews

  • username: user1

         rating: 4

         created_at: 2023-04-19T12:30:00Z

  • username: user2

         rating: 5

         created_at: 2023-05-02T15:00:00Z

{

  "product": {

    "type": "T-Shirt",

    "price": 20.00,

    "sizes": ["S", "M", "L"]

    "reviews": {

      { "username": "user1”, "rating": 4, "created_at": "2023-04-19T12:30:00Z" },

      { "username": "user2”, "rating": 5, "created_at": "2023-05-02T15:00:00Z" }

    }

  }

}

Key differences: YAML vs. JSON

Due to YAML’s syntax, it’s considered easier to read, comprehend, and write a YAML file than JSON, especially for non-developers. Next, we discuss other key differences.

Data types

JSON format supports the following data types:

  • Number
  • Boolean
  • Null
  • String
  • Array
  • Object (collection of nested key-value pairs)

YAML format supports all native data types of any dynamic programming language. For example, it supports collections of nested data made of sequences (lists), scalars (like numbers and strings), and mappings (key-value pairs). This support lets you create more complex data structures. 

Built-in support

JSON is widely used in combination with many different programming languages. For example, JavaScript offers built-in support to parse JSON. The Python Standard Library also includes JSON, and a JSON library is built into PHP 5.2. Similarly, over 50 other popular languages support JSON through various packages and libraries.

In contrast, no popular languages have built-in support for YAML. Instead, developers must install and use libraries. Two popular YAML parsers are PyYAML for Python and LibYAML for C. 

Versioning 

You can version both JSON and YAML through versioning software like Git. However, it’s considered easier to scan and comprehend changes in YAML files between versions.

Comments

There’s no support in JSON data format for comments. Developers must define a comment key-value pair as a way around this.

In a YAML file, comments follow a hash symbol.

When to use YAML vs. JSON

Thanks to pervasive support and integration with JavaScript, JSON is a more popular data serialization format for most use cases over YAML. JSON is used extensively in distributed software communications, web applications, configuration files, and APIs.

While YAML may appear to be a better choice based on data typing and its human-readable format, JSON is typically preferred for cross-compatibility. This is because many applications and services already parse JSON data format.

On the other hand, YAML has gained a strong presence in particular areas of computing because of its readability and support of comments. Notably, YAML is the primary data serialization format for configuration files in many automation, DevOps, and infrastructure as code (IaC) tools and services. For instance, YAML is often used in Docker and Kubernetes files.

YAML is also used to write configuration files in these Amazon Web Services (AWS) offerings: AWS CloudFormation, AWS Serverless Application Model (AWS SAM), and AWS CodeBuild.

Summary of differences: YAML vs. JSON

 

JSON

YAML

What is it?

A data serialization format to exchange structured data between software applications and services. Prioritizes application use over human use. 

A data serialization format to exchange structured data between software applications and services. Prioritizes human use over application use.

Main use cases

Widespread across platforms, languages, distributed software communications, web apps, config files, and APIs.

Configuration files in many automation, DevOps, and infrastructure as code (IaC) tools and services.

Readability

Easy.

Easiest.

Data types

Number, Boolean, null, string, array, and object.

Supports all data types through collections of nested data comprising sequences, scalars, and mappings.

Supports comments

No.

Yes.

Supports data objects as values

Yes.

No.

Versioning

Yes, but it’s not as simple to scan and comprehend differences between versions at a glance.

Yes, and it’s simple to scan and comprehend differences between versions at a glance.

How can AWS support your YAML and JSON requirements?

All AWS data integration services can process JSON files. Here are three relevant AWS services:

  • AWS Glue is a serverless data integration service. You can use AWS Glue to discover, prepare, and combine data for analytics, machine learning, and application development.
  • Amazon Simple Queue Service (Amazon SQS) is a fully managed message queuing service. You can use it to send, store, and receive messages between software components at any volume. Amazon SQS messages contain up to 256 KB of text data, including XML, JSON, and unformatted text.
  • Amazon DocumentDB (with MongoDB compatibility) is a fully managed native JSON document database. Amazon DocumentDB makes operating critical document workloads at virtually any scale easy and cost-effective without managing infrastructure.

Similarly, developer tools on AWS support YAML files in several deployment services. Here are two examples:

  • AWS Step Functions allows you to build resilient serverless workflows using AWS services—such as AWS Lambda, Amazon Simple Notification Service (Amazon SNS), and Amazon DynamoDB. Step Functions also supports YAML state machine definitions. This makes it possible to define workflows in the same language as your infrastructure as code (IaC).
  • AWS App2Container is a command line tool that you can use to migrate and modernize Java and .NET web applications into container format. It generates YAML files for Amazon Elastic Container Service (Amazon ECS) task definitions and Kubernetes deployment. It also follows AWS best practices for security and scalability.

Get started with YAML and JSON on AWS by creating an account today.