How to Convert JSON to YAML in Python 3

Nowadays, the use of YAML as data serialization has increased significantly. And because of its readability and ease of use, it becomes an excellent alternative to JSON. Even though JSON format is widely used to build many applications at present, there are times when we might need to convert JSON data to YAML.

In this article, we will learn how we can convert JSON data to YAML using Python.

What is JSON?

JSON stands for Javascript Object Notation. It is the data interchange format that is easy to read and write. JSON is primarily used to transmit serialized structured data from a web-server to a web application as an alternative to XML.

What is YAML?

YAML stands for Yet Another Markup Language. It is a human-readable data-serialization language that is commonly used to write configuration files and in applications where data are stored or transmitted.

It is a superset of JSON which means we can parse any JSON data to YAML easily using a YAML parser.

Converting JSON to YAML in Python

In python, we get several libraries that we can use to convert JSON to YAML format. We will use the PyYAML library in this article.

First, we have to install the PyYAML library. To install open your terminal and type the following:

pip install PyYAML

Once installed successfully, we can now use the yaml library functions in the code.

Now, to convert the JSON data to YAML, we have to first load the JSON data using the json library.

Next, we have to use the dump() method from the PyYAML library to convert all the JSON data into the YAML format.

Let’s check the code below to understand.

import json
import yaml

# JSON data
json_data = '{"name": "Johnny", "age": 35, "country": "Germany"}'

# Load JSON data into Python dictionary
data = json.loads(json_data)

# Convert dictionary to YAML
yaml_data = yaml.dump(data)

# Print YAML data
print(yaml_data)

Output:

age: 35
country: Germany
name: Johnny

In the above code,

We have defined the JSON data as a string and stored it in the variable json_data . Next, we have used json.loads() method to convert the json data to Python dictionary.

And finally, using the yaml.dump() method we have converted the dictionary to YAML format. So, the YAML output is printed out on the console/terminal.

Conclusion:

In this article, we have learned how to convert JSON to YAML in python using the PyYAML library. It is very useful in situations where we have to convert data between different serialization formats while working on an application.

FAQs

1. What’s the difference between JSON and YAML format?

The key difference between JSON and YAML are:

  1. JSON syntax is based on Javascript object literals, while YAML has a more human-readable syntax that is based on indentation and whitespace.
  2. JSON has fixed data types such as strings, numbers, booleans, and null values. Whereas, YAML has a more extensible data model that allows for custom data types.
  3. YAML supports comments whereas JSON does not support comments.

2. Can YAML parse JSON data?

Yes, since YAML is a superset of JSON, it can parse JSON data without any modification easily.

3. Is YAML better than JSON?

It depends on the user’s use case. The YAML format is used because of its readability and ease of use, while on the other hand, JSON format is a widely used format that has support across different programming languages.

Scroll to Top