What is The Difference Between YAML and JSON?

What is The Difference Between YAML and JSON?

Learn about the differences between YAML and JSON through quick and practical examples. What is The Difference Between YAML vs. JSON?

1. Overview

In this quick article, we'll take a look at the differences between YAML and JSON through quick and practical examples.

2. Format

To have a better image, let's start by looking at the JSON and YAML representations of a simple POJO:

class Person {
        String name;
        Integer age;
        List<String> hobbies;
        Person manager;
    }

First, let's look at its JSON representation:

{
        "name":"John Smith",
        "age":26,
        "hobbies":[
            "sports",
            "cooking"
        ],
        "manager":{
            "name":"Jon Doe",
            "age":45,
            "hobbies":[
                "fishing"
            ],
            "manager":null
        }
    }

JSON syntax is somewhat cumbersome as it uses special syntax like curly braces {} and square brackets [] to represent objects and arrays.

Next, let's see how the same structure would look in YAML:

name: John Smith
    age: 26
    hobbies:
      - sports
      - cooking
    manager:
      name: Jon Doe
      age: 45
      hobbies:
        - fishing
      manager:

YAML's syntax looks a bit friendlier as it uses blank spaces to denote relations between objects and ‘–‘ to represent array elements.

We can see that although both are easily readable, YAML tends to be more human-readable.

Another bonus point for YAML is the number of lines it takes to represent the same information — YAML takes only 11 lines, while JSON takes 16.

data json javascript developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.

Data Lakes Are Not Just For Big Data - DZone Big Data

A data expert discusses the three different types of data lakes and how data lakes can be used with data sets not considered 'big data.'

JSON Tutorial For Beginners | What is JSON | Learning JSON with JavaScript

JSON Tutorial For Beginners | What is JSON | Learning JSON with JavaScript

How to Create a JavaScript Radar Chart

A data visualization developer explains what a Radar Chart is and how to build such interactive graphics using JavaScript / HTML5.

Working With Data & APIs in JavaScript - JSON Data

Welcome to Working with Data and APIs in JavaScript! Learn how to work with JSON data from an API including how to load JSON data with fetch() and update DOM element. Learn how to work with data in web applications. How do you retrieve, collect, and store data?