"YAML is user-friendly, but why does JSON hold the throne of data exchange?"
"Where is YAML strong, and why was JSON destined to win?"


🔥 1. The Prelude to the Data Format War

Once upon a time, programmers needed a way to store and exchange data.
In the early days, XML held the throne.

<person>
    <name>Alice</name>
    <age>25</age>
    <skills>
        <skill>Python</skill>
        <skill>Django</skill>
    </skills>
</person>

However, XML was too verbose and heavy, making it hard to read.
This led to the emergence of JSON and YAML.


🤖 2. JSON: Seizing the Throne of Data Exchange

In 2001, Douglas Crockford introduced JSON, derived from JavaScript's object representation.
"A simple, lightweight, and machine-readable data exchange format!"

The goal of JSON was clear:
A structure that can be read by humans, but better understood by machines
Optimized for data transmission between browsers and servers
Interfaces seamlessly with object-oriented languages (JavaScript, Python, etc.)

Example of JSON:

{
    "name": "Alice",
    "age": 25,
    "skills": ["Python", "Django"]
}
  • It could express data structures solely with {} and [],
  • JavaScript on the web could handle JSON natively, leading to its rapid popularity.
  • Especially as it became the standard data format in REST APIs, JSON claimed its throne.

🧐 3. YAML: A Data Format That’s Easy for Humans to Understand

However, there were people who wanted a format that is easier for humans to read than JSON.
"Wouldn’t it be better to express data structures without unnecessary braces and quotes?"

Thus, in 2001, YAML (Ain’t Markup Language) came to be.

name: Alice
age: 25
skills:
  - Python
  - Django

Characteristics of YAML:
Easy for humans to read (neat without braces or quotes!)
Supports comments (using #)
Convenient for configuration files

Because of this, YAML became the champion of configuration files.
It is used as the default format in places like Kubernetes, Docker, Ansible, CI/CD, and GitHub Actions!

However, in the data exchange market, YAML could not defeat JSON.
Why not? 🤔


🤯 4. Why Couldn’t YAML Beat JSON?

If YAML was more readable for humans, it might have been more widespread than JSON…
But the reality was otherwise.

✅ Reasons JSON Triumphed:
1. Is the syntax too lenient?
- JSON has a clear structure with {} and [],
- YAML uses indentation, leading to a higher chance of errors.
- A single misplaced space can cause errors! (Especially the Tab vs Space issue in YAML 🤯)

  1. Parsing speed is slower
  2. JSON can be parsed quickly, while
  3. YAML, being flexible in syntax, has many interpretations which slow it down.
  4. YAML is less efficient with large data compared to JSON.

  5. YAML has security issues

  6. YAML can include executable code (!python/object), leading to security risks (RCE, remote code execution).
  7. JSON is safe as it is pure data!

  8. Not natively supported in browsers

  9. JSON is the default object structure in JavaScript, while
  10. YAML requires separate libraries to be used.

Ultimately, JSON was chosen as the standard for web and data exchange.


🚀 5. But YAML Holds the Throne of Configuration Files!

That doesn't mean YAML has disappeared. If JSON has become the standard for data exchange, YAML has become the standard for configuration files.

🔥 Reasons for YAML's Strength:

  • Comment support (#) → convenient for writing configuration files
  • Easy for humans to read and modify
  • Used as a core format in DevOps, such as Kubernetes, Docker, Ansible

Real-life Use Cases:

  • docker-compose.yml
  • kubernetes.yml
  • github-actions.yml
  • ansible-playbook.yml

However, using YAML for data exchange has its caveats. Especially in Django Rest Framework (DRF), you can configure the serialization method to YAMLRenderer, but the default is still JSONRenderer.

from rest_framework.renderers import JSONRenderer, YAMLRenderer
from rest_framework.response import Response
from rest_framework.views import APIView

class ExampleView(APIView):
    renderer_classes = [JSONRenderer, YAMLRenderer]

    def get(self, request):
        data = {"message": "Hello, YAML!"}
        return Response(data)

But most APIs default to JSON! Because YAML is not widely used due to parsing speed and security issues.

JSON vs YAML Medieval Battle


🎯 6. Conclusion: Which is Better, JSON or YAML?

✅ When to Choose JSON:
- Web APIs, data exchange (where speed & reliability are crucial!)
- Storing NoSQL data
- When using directly in a browser

✅ When to Choose YAML:
- Configuration files (Kubernetes, Docker, CI/CD)
- Writing documents that are easy for humans to read
- DevOps-related tasks

One-line summary:
🚀 "JSON is the king of data exchange, and YAML is the king of configuration files!"