Convert JSON Data To YAML in Python
Tags: python, opensource, programming, tutorial
Published: 2024-01-06 08:48:10

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/](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](https://wahyounote.wordpress.com/2024/01/10/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:

```json
{
  "person": {
    "name": "John Doe",
    "address": {
      "street": "123 Main St",
      "city": "Anytown",
      "state": "CA"
    }
  }
}
```

**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](https://wahyounote.wordpress.com/2024/01/10/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**
1. A superset of json, meaning any valid json file is also a valid yaml file.
2. Support various data types including strings, numbers, booleans, lists, dictionaries, and often custom types.
3. Allows for anchors and aliases to reference data elsewhere in the document, reducing redundancy.

- **Common Uses**
1. Configuration files for applications and servers.
2. Data exchange between programs and services.
3. Storing application state and settings.
4. Defining automation scripts and playbooks.

Here is the yaml example data:

```yaml
name: My Application
version: 1.0.0
settings:
  debug: true
  port: 8080
data:
  users:
  - name: John Doe
    email: john.doe@example.com
```

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:

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

```python
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)
```
We can also convert yaml data into json format using `yaml_to_json` function just like the example bellow.

```python
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)
``` 

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](https://github.com/aliftech/FormatFusion/blob/master/DOCUMENTATION.md). 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i4z2cl8dkhjgs0jf0k8s.png)

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!