---
title: "JSON Vs. YAML Vs. TOML: How to Use in Python"
description: "JSON, YAML, and TOML are data serialization formats commonly used for configuration files and data exchange. JSON is strict, YAML is human-readable, and TOML prioritizes simplicity and readability."
url: https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/
date: 2024-09-17
modified: 2026-10-10
author: "Srini"
image: https://i0.wp.com/srinimf.com/wp-content/uploads/2024/09/image-18.png?fit=1024%2C768&ssl=1
categories: ["Python"]
tags: ["JSON VS YAML VS TOML"]
type: post
lang: en
---

# JSON Vs. YAML Vs. TOML: How to Use in Python

**JSON, YAML, and TOML** are all **data serialization** formats. They are commonly used for configuration files. They are also used for data exchange between systems and storing structured data. Here are their differences and syntax examples.

![JSON VS TAML VS TOML](https://i0.wp.com/srinimf.com/wp-content/uploads/2024/09/pexels-photo-9721365.jpeg?resize=1880%2C1253&ssl=1)*Photo by cottonbro studio on Pexels.com*

Table of contents

1. [Serialization formats: JSON Vs YAML Vs TOML](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#serialization-formats-json-vs-yaml-vs-toml)
   1. [Differences](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#differences)
   2. [Syntax Examples:](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#syntax-examples)
      1. [JSON Example:](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#json-example)
      2. [YAML Example:](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#yaml-example)
      3. [TOML Example:](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#toml-example)
   3. [Key Points:](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#key-points)
   4. [Use Cases for JSON, YAML, and TOML](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#use-cases-for-json-yaml-and-toml)
      1. [1. JSON (JavaScript Object Notation):](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#1-json-javascript-object-notation)
      2. [2. YAML (YAML Ain’t Markup Language):](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#2-yaml-yaml-ain-t-markup-language)
      3. [3. TOML (Tom’s Obvious, Minimal Language):](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#3-toml-tom-s-obvious-minimal-language)
   5. [Choosing the Right Format:](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#choosing-the-right-format)
   6. [1. Reading JSON Data in Python](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#1-reading-json-data-in-python)
      1. [Example JSON File (data.json):](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#example-json-file-data-json)
      2. [Python Code to Read JSON:](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#python-code-to-read-json)
   7. [2. Reading YAML Data in Python](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#2-reading-yaml-data-in-python)
      1. [Example YAML File (data.yaml):](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#example-yaml-file-data-yaml)
      2. [Python Code to Read YAML:](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#python-code-to-read-yaml)
   8. [3. Reading TOML Data in Python](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#3-reading-toml-data-in-python)
      1. [Example TOML File (data.toml):](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#example-toml-file-data-toml)
      2. [Python Code to Read TOML:](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#python-code-to-read-toml)
   9. [Summary:](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/#summary)

## **Serialization formats: JSON Vs YAML Vs TOML**

### **Differences**

1. **JSON (JavaScript Object Notation):**
   - **Format:** Text-based, uses a subset of JavaScript syntax.
   - **Readability:** **Less human-readable**, especially for complex structures.
   - **Data Types:** Supports strings, numbers, arrays, objects (dictionaries), booleans, and null.
   - **Comments:** This does not support comments.
   - **Use Cases:** Web APIs, data interchange between server and client, configuration files.
2. **YAML (YAML Ain’t Markup Language):**
   - **Format:** Human-readable, uses indentation and a clean, **minimalistic syntax.**
   - **Readability:** Very human-readable and easy to edit.
   - **Data Types:** Supports strings, numbers, lists, dictionaries, booleans, and null.
   - **Comments:** Supports comments using `#`.
   - **Use Cases:** Configuration files (e.g., Docker Compose, Ansible playbooks).
3. **TOML (Tom’s Obvious, Minimal Language):**
   - **Format:** Designed to be minimal and easily readable, with an INI-like syntax.
   - **Readability:** Human-readable, **intended to be unambiguous**.
   - **Data Types:** Supports strings, numbers, arrays, tables (dictionaries), booleans, dates, and times.
   - **Comments:** Supports comments using `#`.
   - **Use Cases:** Configuration files for applications (e.g., Python’s `pyproject.toml`).

### **Syntax Examples:**

#### **JSON Example:**

```
{
  "name": "Example Project",
  "version": 1.0,
  "dependencies": {
    "library1": "1.2.3",
    "library2": "4.5.6"
  },
  "is_active": true,
  "tags": ["example", "json", "config"]
}
```

#### **YAML Example:**

```
name: Example Project
version: 1.0
dependencies:
  library1: "1.2.3"
  library2: "4.5.6"
is_active: true
tags:
  - example
  - yaml
  - config
```

#### **TOML Example:**

```
name = "Example Project"
version = 1.0
is_active = true

[dependencies]
library1 = "1.2.3"
library2 = "4.5.6"

tags = ["example", "toml", "config"]
```

### **Key Points:**

- **JSON** is strict, less human-friendly, and commonly used in web contexts.
- **YAML** is more human-readable with support for comments, but its reliance on indentation can lead to syntax errors.
- **TOML** aims for simplicity and readability with a clear and unambiguous syntax.

Also, read: [50 Python Tips and Tricks You Should Read Right Away](https://srinimf.com/2023/08/17/50-comprehensive-python-tips-and-tricks-to-read-now/)

### **Use Cases for JSON, YAML, and TOML**

#### **1. JSON (JavaScript Object Notation):**

- **Web APIs and Data Interchange:** JSON is widely used to send data between a client and server in web applications. It is also widely used for receiving data. It’s supported by most programming languages and is ideal for RESTful APIs.
- **Configuration Files:** JSON is often used in configuration files for tools and applications. Especially in environments where human readability is less of a priority (e.g., `package.json` for Node.js projects).
- **Data Storage:** JSON is used for storing structured data: in NoSQL databases like MongoDB.
- **Logging and Data Serialization:** Ideal for logging and serializing data structures due to its compatibility with many languages and platforms.

#### **2. YAML (YAML Ain’t Markup Language):**

- **Configuration Files:** YAML is popular for configuration files in many tools and platforms because of its human-readable format. Examples include:
  - **DevOps Tools:** Docker Compose (`docker-compose.yml`), Kubernetes manifests.
  - **CI/CD Pipelines:** Configuration for services like GitLab CI, CircleCI, and Ansible playbooks.
  - **Programming Frameworks:** Configurations for frameworks like Ruby on Rails (`database.yml`).
- **Data Serialization:** Sometimes used for data serialization in scenarios where readability by humans is important, like in collaborative projects.
- **Document Markup:** YAML can be used for simple document markup or defining templates for content management systems.

#### **3. TOML (Tom’s Obvious, Minimal Language):**

- **Configuration Files:** TOML is designed to be a simple and unambiguous format for configuration files. It prioritizes clarity and ease of editing. Examples include:
  - **Software Projects:** Python’s `pyproject.toml` for specifying build system requirements.
  - **Rust Projects:** Cargo’s configuration (`Cargo.toml`) for Rust projects.
- **Environment Configuration:** Often used in projects where configuration needs to be human-readable and editable. This prevents introducing errors. An example is defining environment variables.
- **Application Settings:** Useful for defining settings for applications that need to be adjusted or fine-tuned by users or administrators.

### **Choosing the Right Format:**

- Use **JSON** when you need a widely supported format for data interchange. This is especially useful in web APIs and systems where human readability is less important.
- Use **YAML** for configuration files where readability is essential. It also supports complex structures. This is especially useful in DevOps and infrastructure as code (IaC) scenarios.
- Use **TOML** when you want a balance between readability and simplicity. It is especially useful for project configuration files in environments like Python and Rust.

Here are examples of how to read data from JSON, YAML, and TOML files [in Python](https://srinimf.com/2024/02/23/pycache-folder-in-python-how-to-access-its-location/).

### **1. Reading JSON Data in Python**

[You can use](https://srinimf.com/2021/01/19/python3-how-to-install-beautifulsoup-parser/) the built-in `json` module to read JSON data.

#### **Example JSON File (`data.json`):**

```
{
  "name": "Example Project",
  "version": 1.0,
  "dependencies": {
    "library1": "1.2.3",
    "library2": "4.5.6"
  },
  "is_active": true,
  "tags": ["example", "json", "config"]
}
```

#### **Python Code to Read JSON:**

```
import json

# Reading JSON data from a file
with open('data.json', 'r') as file:
    data = json.load(file)

# Accessing data
print(data['name'])         # Output: Example Project
print(data['dependencies']) # Output: {'library1': '1.2.3', 'library2': '4.5.6'}
```

### **2. Reading YAML Data in Python**

To read YAML data, you need the `PyYAML` package, which you can install with `pip install pyyaml`.

#### **Example YAML File (`data.yaml`):**

```
name: Example Project
version: 1.0
dependencies:
  library1: "1.2.3"
  library2: "4.5.6"
is_active: true
tags:
  - example
  - yaml
  - config
```

#### **Python Code to Read YAML:**

```
import yaml

# Reading YAML data from a file
with open('data.yaml', 'r') as file:
    data = yaml.safe_load(file)

# Accessing data
print(data['name'])         # Output: Example Project
print(data['dependencies']) # Output: {'library1': '1.2.3', 'library2': '4.5.6'}
```

### **3. Reading TOML Data in Python**

To read TOML data, you need the `toml` package, which you can install with `pip install toml`.

#### **Example TOML File (`data.toml`):**

```
name = "Example Project"
version = 1.0
is_active = true

[dependencies]
library1 = "1.2.3"
library2 = "4.5.6"

tags = ["example", "toml", "config"]
```

#### **Python Code to Read TOML:**

```
import toml

# Reading TOML data from a file
with open('data.toml', 'r') as file:
    data = toml.load(file)

# Accessing data
print(data['name'])         # Output: Example Project
print(data['dependencies']) # Output: {'library1': '1.2.3', 'library2': '4.5.6'}
```

### **Summary:**

- **JSON**: Use the `json` module, which is built into Python.
- **YAML**: Use the `PyYAML` package (`yaml` module).
- **TOML**: Use the `toml` package (`toml` module).

### Share this:

-
- [Tweet](https://twitter.com/share)
- [![](https://i0.wp.com/assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png)](https://www.pinterest.com/pin/create/link/?url=https%3A%2F%2Fsrinimf.com%2F2024%2F09%2F17%2Fjson-vs-yaml-vs-toml-how-to-use-in-python%2F&media=https%3A%2F%2Fi0.wp.com%2Fsrinimf.com%2Fwp-content%2Fuploads%2F2024%2F09%2Fimage-18.png%3Ffit%3D1024%252C768%26ssl%3D1&description=JSON%20Vs.%20YAML%20Vs.%20TOML%3A%20How%20to%20Use%20in%20Python)
-
- [Share on Tumblr](https://www.tumblr.com/share)
- [Share on WhatsApp (Opens in new window) WhatsApp](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/?share=jetpack-whatsapp)
- [More](#)
-

- [Print (Opens in new window) Print](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/?share=print)
- [Share on Reddit (Opens in new window) Reddit](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/?share=reddit)
- [Share on Telegram (Opens in new window) Telegram](https://srinimf.com/2024/09/17/json-vs-yaml-vs-toml-how-to-use-in-python/?share=telegram)
- [Email a link to a friend (Opens in new window) Email](mailto:?subject=%5BShared%20Post%5D%20JSON%20Vs.%20YAML%20Vs.%20TOML%3A%20How%20to%20Use%20in%20Python&body=https%3A%2F%2Fsrinimf.com%2F2024%2F09%2F17%2Fjson-vs-yaml-vs-toml-how-to-use-in-python%2F&share=email)
-

### Like this:

Like Loading…
