How to Convert YAML to JSON [Python, PowerShell, Go]

Published:20 April 2021 - 8 min. read

Anthony Metcalf Image

Anthony Metcalf

Read more tutorials by Anthony Metcalf!

Azure Cloud Labs: these FREE, on‑demand Azure Cloud Labs will get you into a real‑world environment and account, walking you through step‑by‑step how to best protect, secure, and recover Azure data.

Many modern tools, particularly in configuration management and Infrastructure as Code (IaC) spaces, use JSON or YAML files to store configuration data. If you need to convert data from YAML to JSON format, then this article is for you.

In this article, you will learn several ways to convert data YAML to JSON format, including scripting in Python, PowerShell, and Go.

Prerequisites

If you’d like to follow along with examples in this tutorial, be sure you have

  • A code editor. This article will use Visual Studio Code version 1.55.1. Feel free to use any code editor you want.
  • To get started with converting YAML to JSON, you need a sample YAML content. Copy the content below into a new file called operating-systems.yml. Save the YAML file in your working directory.
operating-systems.yml
Windows 10: 100
Windows Server 2019: 50
Windows Server 2022: 1
MacOS: 3
CentOS: 75
Photon: 12

Using Python to Convert YAML to JSON

Python is an excellent language to use for automation. But did you know you can extend Python’s capabilities and use it to convert documents from YAML to JSON? And you only need to perform a few steps and start converting YAML to JSON in no time.

The example in this section requires you to have installed Python 3.9 and PIP (the latest version as of this writing is 21.0.1).

Adding YAML Support to Python

Python does not have out-of-the-box support for YAML. Meaning, Python cannot read or interpret YAML documents by default. To add YAML support to Python, you have first to install the PyYAML module.

To install the PyYAML module, you’ll need to run the pip command, which is the package installer for Python. To do so, follow these steps.

First, open your preferred command interpreter, such as the command prompt or PowerShell. The pip command should work in either one. This example uses PowerShell.

Next, run the command below in PowerShell. This command installs the PyYAML module.

pip install pyyaml

As you can see in the screenshot below, pip installed the latest version of PyYAML (5.4.1 as of this writing).

Installing the PyYAML module
Installing the PyYAML module

Writing the Conversion Script

Now that you’ve installed the required module (PyYAML), you’re ready to write your conversion script. To create the YAML to JSON Python script, follow these steps.

1. Open your code editor and create a new file called convert-os.py in your working directory. This file is your script.

2. Copy the code below and paste it into your blank convert-os.py file. This script will read the YAML content of operating-systems.yml, convert the content to JSON format, and write the JSON output to the python_operating-systems.json file.

## convert-os.py
## Import the modules to handle JSON & YAML
import yaml
import json

## Create a variable to hold the data to import
os_list = {}

## Read the YAML file
with open("c:\temp\operating-systems.yml") as infile:
     # Marshall the YAML into the variable defined above os_list = yaml.load(infile, Loader=yaml.FullLoader)     # Print the List to the console. print(os_list)
 Open a file to write the JSON output. The 'w' makes the file writable
 with open("c:\temp\python_operating-systems.json", 'w') as outfile:
     # Marshall the JSON, setting "indent" makes the file more readable json.dump(os_list, outfile, indent=4) print("JSON file written.")

3. Save the convert-os.py script.

Running the Conversion Script

After creating the conversion script, it’s time to put it to the test. To execute the YAML to JSON Python script, proceed as follows:

1. Open a terminal window. This article uses PowerShell, but the command prompt (CMD) would work just as well.

2. Copy the command below, paste it in PowerShell, and press Enter. This command runs the Python executable program to invoke the convert-os.py script you created in the previous section.

python c:\temp\convert-os.py

After running the command, you should see a similar output like the image below. As you can see, the command displayed the JSON output on the screen and saved the same output to the c:\temp\python_operating-systems.json file.

Running the YAML to JSON conversion script using Python
Running the YAML to JSON conversion script using Python

3. Lastly, open the JSON file called python_operating-systems.json that the convert-os.py Python script should have created. In this example, to open the file in notepad, run the command below in PowerShell.

notepad C:\temp\python_operating-systems.json
Viewing the python_operating-systems.json file in notepad
Viewing the python_operating-systems.json file in notepad

Using PowerShell to Convert YAML to JSON

PowerShell is capable of modifying text files and converting objects to several formats in including JSON. Because PowerShell is extensible, you can use PowerShell to convert YAML to JSON format with the right module.

Adding YAML Support to PowerShell

PowerShell has built-in support for handling JSON content, but not for YAML. Fortunately, there is a module that extends PowerShell to support YAML files. This module is called PowerShell-yaml, and the latest version as of this writing is 0.4.2. Follow these steps to install the module.

1. On your computer, open a PowerShell session.

2. Copy the command below, paste it in PowerShell, and press Enter. This command uses the Install-Module cmdlet to install the PowerShell-yaml module.

Install-Module PowerShell-yaml

3. After installing the module, two new cmdlets will become available in PowerShell as part of the PowerShell-yaml module. These new cmdlets are:

ConvertFrom-Yaml – the cmdlet to convert YAML data to a hash table.

ConvertTo-Yaml – the cmdlet to convert hash table objects to YAML data.

To confirm both cmdlets are available in PowerShell, run the command below in PowerShell.

Get-Command -Module PowerShell-yaml

As you can see below, the Get-Command cmdlet listed the cmdlets from the PowerShell-yaml module.

Getting the PowerShell-yaml cmdlets
Getting the PowerShell-yaml cmdlets

Writing the Conversion Script

To create the YAML to JSON conversion PowerShell script, follow these instructions.

1. Open your code editor and create a new file called convert-os.ps1 in your working directory.

2. Copy code below and paste in the blank convert-os.ps1 file. The code snippet below reads the operating-systems.yml file and converts it to JSON. Once in JSON, it then stores the JSON in the file PowerShell_operating-systems.json.

#convert-os.ps1
 #Read the YAML file using Get-Content and convert the data to a hashtable using ConvertFrom-Yaml. The $os_list variable stores the hashtable object.
 $os_list = (Get-Content -Path "C:\temp\operating-systems.yml" | ConvertFrom-Yaml)
 #Convert the hashtable object in the $os_list variable to JSON format using ConvertTo-Json. Once in JSON, save the save to C:\temp\PowerShell_operating-systems.json file using Set-Content.
 Set-Content -Path "C:\temp\PowerShell_operating-systems.json" -Value ($os_list | ConvertTo-Json)

3. Save the convert-os.ps1 file.

Running the Conversion Script

Now that you’ve created your conversion script, the next step is to run it to convert YAML to JSON. To execute the PowerShell conversion script, follow these steps.

  1. Open a PowerShell window if you don’t have one opened yet.

2. Copy the command below, paste it in PowerShell, and press Enter. This command invokes the convert-os.ps1 script you’ve created in the previous section.

c:\temp\convert-os.ps1

3. After running the script, open the JSON output file C:\temp\PowerShell_operating-systems.json that the convert-os.ps1 script created in the previous step. To do so, run the command below in PowerShell to open the PowerShell_operating-systems.json in notepad.

notepad C:\temp\PowerShell_operating-systems.json
Viewing the content of the PowerShell_operating-systems.json file
Viewing the content of the PowerShell_operating-systems.json file

Using Go to Convert YAML to JSON

Python and PowerShell are both high-level languages. Being high-level makes writing scripts easier but also takes away a little bit of control. On the other hand, Go is a low-level language, which makes importing YAML data more complex.

The example in this section will be using Go. The latest version as of this writing is go1.16.3. If you don’t have Go yet, refer to the download and install page to install it on your computer.

Adding YAML Support to Go

Like PowerShell and Python, JSON support is part of Go’s core libraries, but YAML is not. To add YAML support in Go, you need to install the YAML.v3 package first using the go get command. To install the YAML.v3 package:

First, on your computer, open the command shell you want to use, such as the command prompt or PowerShell. This example uses PowerShell.

Next, copy the command below and run it in PowerShell. This command will download and install the package name gopkg.in/yaml.v3.

go get gopkg.in/yaml.v3
Installing the yaml.v3 package in Go
Installing the yaml.v3 package in Go

Writing the Conversion Script

To create the YAML to JSON conversion Go script, follow these instructions.

1. Open your code editor and create a new file called convert-os.go in your working directory.

2. Copy the code below and paste in the blank convert-os.go file. The code imports the required packages, defines some memory structures, imports the YAML file, and converts to JSON before writing to a JSON file called c:\temp\go_operating-systems.json.

// This tells go which function to load.
 package main
 // Import packages:
 import (
  // JSON module
     "encoding/json"
  // For writing output to the screen
     "fmt"
  // For reading and wiritng files
     "io/ioutil"
 // YAML module
     "gopkg.in/yaml.v3"
 )
 // Define two "Structs" these are data structures in memory, and match
 // The form of the YAML and JSON files.
 type operatingSystems struct {
     Windows10         int yaml:"Windows 10"
     WindowsServer2019 int yaml:"Windows Server 2019"
     WindowsServer2022 int yaml:"Windows Server 2022"
     MacOS             int yaml:"MacOS"
     CentOS            int yaml:"CentOS"
     Photon            int yaml:"Photon"
 }
 type operatingSystemsjson struct {
     Windows10         int json:"Windows 10"
     WindowsServer2019 int json:"Windows Server 2019"
     WindowsServer2022 int json:"Windows Server 2022"
     MacOS             int json:"MacOS"
     CentOS            int json:"CentOS"
     Photon            int json:"Photon"
 }
 func main() {
   // Let the user know the process has started
     fmt.Println("Parsing YAML file")
 // Define the path to the input file
 var fileName string = "c:\temp\operating-systems.yml"
 // Load the YAML from the file. Go requires error handling for this step.
 yamlFile, err := ioutil.ReadFile(fileName)
     if err != nil {
         fmt.Printf("Error reading YAML file: %s\n", err)
         return
     }
 // Extract the YAML into your Struct
     var oses operatingSystems
     yaml.Unmarshal(yamlFile, &oses)
 //Create the JSON Struct, using the data from the YAML Struct
 var osesjson = operatingSystemsjson{
         Windows10:         oses.Windows10,
         WindowsServer2019: oses.WindowsServer2019,
         WindowsServer2022: oses.WindowsServer2022,
         MacOS:             oses.MacOS,
         CentOS:            oses.CentOS,
         Photon:            oses.Photon,
     }
 // Create a string to output in JSON format.
 jsonOutput, err := json.Marshal(osesjson)
 // Print the result to screen. Notice that the %+v means that 
   // the variable name gets printed with the data. This is why
   // there are no spaces in the output Kay names.
     fmt.Printf("Result: %+v\n", osesjson)
 //write the JSON file
 err = ioutil.WriteFile("c:\temp\Go_operating-systems.json", jsonOutput, 0644)
 }

3. Save the convert-os.go file.

Running the Conversion Script

Running Go scripts uses the go run command followed by the script’s filename. To run the YAML to JSON conversion Go script, proceed as follows.

1. On your computer, open the command shell you want to use. On Windows, the go will work on either the command prompt or PowerShell. This example uses PowerShell to run go.

2. Once you’re in PowerShell, copy the command below and run it in PowerShell. This command will invoke the go run command to run the c:\temp\convert-os.go script.

go run c:\temp\convert-os.go
Running Convert-os Go script.
Running Convert-os Go script.

3. After running the script, open the output file C:\temp\Go_operating-systems.json that the convert-os.go script created in the previous step. This example opens the Go_operating-systems.json in notepad.

Viewing the Go_operating-systems.json in notepad
Viewing the Go_operating-systems.json in notepad

Conclusion

In this article, you’ve learned how to convert YAML data and files to JSON using scripts in Python, PowerShell, and Go. Now that you’ve learned different ways to convert YAML to JSON, which way do you think you will use more?

Hate ads? Want to support the writer? Get many of our tutorials packaged as an ATA Guidebook.

Explore ATA Guidebooks

Looks like you're offline!