DEV Community

Wahyu K. A.
Wahyu K. A.

Posted on • Updated on

Convert JSON Data To YAML in Python

Hello everyone, in this session i would like to share a simple tutorial about how to convert json data into yaml. In this tutorial i am gonna use python as programming language and a library called FormatFusion. FormatFusion is a powerful data converter library for python, you can check the detail in this https://pypi.org/project/FormatFusion/.

Before we start the tutorial, it is important to know what is json and yaml.

JSON
JSON stands for Javascript Object Notation. It is a lightweight data-interchange format that's widely used for transmitting data between computers. Think for it as a language for computers to talk to each other in a way that's both easy to understand and efficient to transmit. For further information about json, you can visit JSON: The Lightweight Data-Interchange Format.

Here is some keys to know about json:

  • human-readable: JSON is written in the plaintext, so it is easy for human to read and understand, even if they don't know how to program. This make it a great choice for things like configuration files and API responses.

  • language independent: even though it is based on javascript syntax, json can be used with any programming language. There are libraries and frameworks available for all major languages that can read and write json data.

  • lightweight: JSON files are very small and compact, which make them ideal for transmitting data over the internet. This is why json is often used for web APIs and web applications.

  • structured: JSON data is organized into key-value pairs and arrays. This make it easy to access and manipulate specifict place of data.

Here are some common uses of JSON:

  • web APIs: Many web APIs use json to send and receive data. Example, when you use a weather API to get current temperature, the API will return data in json format.

  • configuration files: Many application use json files to store configuration setting. This make it easy to changes the setting without having to modify the code itself.

  • data storage: JSON can be used to store data in a file or database. This is a common way to store data for web applications.

Here is the json example data:

{
  "person": {
    "name": "John Doe",
    "address": {
      "street": "123 Main St",
      "city": "Anytown",
      "state": "CA"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

YAML
YAML ain't a markup language, though originally it meant Yet Another Markup Language. It is another data serialization language similar to json, often used for configuration files and data exchange. However, unlike json, it prioritize human readability with more intuitive and concise syntax. For further information about yaml, you can check it in my article YAML: The Friendly Face of Data – Unveiling The Depth of Human-Readable Code. Here are some key points about yaml:

  • Human-readable
  1. Use indentation to define structure, similar to python code, make it easier to understand the hierarchy of data.
  2. No need for closing tags or square bracket like json, simplifying the visual layout.
  3. Support comments for annotation, further clarifying the meaning of the data.
  • Powerful and Flexible
  • A superset of json, meaning any valid json file is also a valid yaml file.
  • Support various data types including strings, numbers, booleans, lists, dictionaries, and often custom types.
  • Allows for anchors and aliases to reference data elsewhere in the document, reducing redundancy.

  • Common Uses

  • Configuration files for applications and servers.

  • Data exchange between programs and services.

  • Storing application state and settings.

  • Defining automation scripts and playbooks.

Here is the yaml example data:

name: My Application
version: 1.0.0
settings:
  debug: true
  port: 8080
data:
  users:
  - name: John Doe
    email: john.doe@example.com
Enter fullscreen mode Exit fullscreen mode

We have already talking about both json and yaml. Now, it is the time to go to our main topic. First of all, we need to install FormatFusion library using the following command:

pip install FormatFusion
Enter fullscreen mode Exit fullscreen mode

After that we can use the library to convert our json data into yaml format. Here is the example:

from FormatFusion.json_to_yaml import json_to_yaml

json_data = """{
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}"""

yaml_data = json_to_yaml(json_data)
print(yaml_data)
Enter fullscreen mode Exit fullscreen mode

We can also convert yaml data into json format using yaml_to_json function just like the example bellow.

from FormatFusion.yaml_to_json import yaml_to_json

yaml_data = """
   name: John Doe
   age: 30
   city: New York
"""

json_data = yaml_to_json(yaml_data)
print(json_data)
Enter fullscreen mode Exit fullscreen mode

And that's it. How was it? It was easy, right? Converting JSON to YAML with the FormatFusion library is a breeze. In addition to converting YAML and JSON data, FormatFusion can also be used to convert other data formats. For more information, please visit the FormatFusion documentation on the GitHub repository. That's all for this tutorial. Thank you for reading and I hope it was helpful.

Here is an image of the FormatFusion library documentation:

Image description

Here are some additional tips for converting JSON to YAML:

  • If your JSON data is nested, you can use the indent property to control the indentation level of the YAML output.
  • If your JSON data contains comments, you can use the comment property to specify how to handle them in the YAML output.
  • If your JSON data contains special characters, you can use the escape property to specify how to escape them in the YAML output. I hope this helps!

Top comments (3)

Collapse
 
zied_ghalleb profile image
Zied Ghalleb

Hey Wahyu,
The link for FormatFusion library is not working, could you please fix it ? 🙂

Collapse
 
aliftech profile image
Wahyu K. A.

Hey zied, the link is already fixed

Collapse
 
aliftech profile image
Wahyu K. A.

Thank you for reminding me, I'll fix it soon