Data Structures & Algorithms

How to Convert JSON to YAML

“This article will show you how to convert a JSON-formatted file to YAML. Both of these formats are extremely near to human-readable and share many characteristics. JSON’s superset is YAML, and both are data serialization languages. We will be converting a few JSON files with different data types of variables using online tools and comparing these languages while converting them.”

Example # 01: Converting a JSON File to YAML With Simple String Variable Data

The JSON file will have several string variables with values stored in this example. The variables and their values will be enclosed inside the double quotation, which is the conventional format that is followed in JSON, while the YAML format is free of this constraint as it is very close to the everyday language that we use normally. In the screen below, a JSON formatted file is opened in the Notepad++ IDE.

In the file above, we have three string variables with three different values. This file will be stored in the “.json” format on our desktop. After this, we will navigate to the given link below from our default browser.

Code Beautify

This link will open the online tool for converting JSON files into YAML format by selecting the correct format file and converting the necessary constraints that JSON has over YAML. The window that will display after clicking the link is shown below.

Now we have to upload the JSON file on this tool to convert it into the YAML format. So, we will locate the upload file button, which is between the windows, as seen below:

After clicking this button, a system window will appear, and in this window, we will have all the files that are present on the system. So, we have to locate the JSON file that we created on our desktop and select it on the system window, as shown in the snippet below:

As soon as we upload the file, the conversion tool will convert each line of the script from the JSON format to the YAML format, that in our case is the removing of the double quotes in which every element was enclosed. The result below shows that the conversion was successful.

We can also edit the JSON file from the left window, and the tool will automatically convert the code into the YAML format as it supports live changes. After finalizing all the necessary edits, we can either copy the code in the YAML format or even download the new file with the YAML extension by locating and clicking on the Download button present in between the two windows, as shown below in the snippet:

After clicking on the Download button, we will be able to download the file, and the new file can be opened on the Notepad++ IDE as it allows YAML format files as well, as shown below:

We can see that this converted file has the correct YAML formatting for variables and can easily be used as a support file for any other dependent system.

Example # 02: Converting a JSON File to YAML With Complex Array Data Type Values

In this example, we will use an online application to convert a JSON file into the YAML format, which will include numerous data saved in arrays. The variables and their values will be surrounded by curly brackets, which is the standard style used in JSON. However, the YAML format is free of this limitation because it is extremely near to the daily language that we use. A JSON-formatted file is opened in the Notepad++ IDE in the screenshot below.

In the code above, we have two string variables in array format, each with a distinct value. This file will be saved on our desktop in the “.json” format. Following that, we will use our default browser to visit the supplied URL below:

JSON formatter

This link will take you to an online resource for converting JSON files to YAML format by picking the appropriate format file and translating the required limitations that JSON has over YAML. The window that appears after clicking the link is seen below.

Now we must upload the JSON file to be converted into the YAML format. So, we’ll look for the “Load Data” button, which is located between the panes, as seen below:

On pressing this button, a system window will emerge, displaying all of the files that are currently present on the system. So, as indicated in the extract below, we must locate the JSON file that we prepared on our desktop and select it in the system window.

After uploading the file, the conversion tool will transform each line of the script from JSON to YAML, which in our case means deleting the double quotes that contained each element and rearranging the components in numerous lines, which is the YAML format for arrays. The conversion was effective, as evidenced by the results shown below.

We could also modify the JSON file from the left window, and the tool will convert the code to the YAML format automatically because it enables live modifications. Following the completion of all necessary adjustments, we may either copy the code in YAML format or download the new file with the YAML extension by locating and clicking on the “YAML Download” button located between the two windows, as seen in the clip below:

Upon pressing the “YAML Download” button, the file will be downloaded, and the new file may be viewed in the Notepad++ IDE, which supports YAML format files, as demonstrated below:

This converted file has the correct YAML variable formatting and can be used as a reference package for any other dependent system, as we can see in the above output.

Conclusion

In this article, we discussed and implemented two different examples of converting JSON formatted files with distinct values and data types into the YAML format. JSON and YAML script has many similarities, but the YAML format is more inclined towards simplicity as it is very close to the everyday written language, and both are used as a support or data exchange format so they can be easily converted into each other’s format using widely available tools.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.