---
title: "How to Convert YAML to JSON [Python, PowerShell, Go]"
description: "Learn several ways to convert data YAML to JSON format, including scripting in Python, PowerShell, and Go."
canonical: "https://adamtheautomator.com/yaml-to-json/"
---

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

> Learn several ways to convert data YAML to JSON format, including scripting in Python, PowerShell, and Go.

Source: https://adamtheautomator.com/yaml-to-json/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![How to Convert YAML to JSON \[Python, PowerShell, Go\]](https://adamtheautomator.com/wp-content/uploads/2021/04/How-to-Convert-YAML-to-JSON-Python-and-PowerShell.jpg)

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

[![](https://secure.gravatar.com/avatar/6fac870bd46517f209a580d0af97a407e8dbf66931a2417ce76d0a09198a43b4?s=192&d=mm&r=g)Anthony Metcalf](https://adamtheautomator.com/author/anthony-metcalf/)20 April 20218 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Go](/tag/go/)[JSON](/tag/json/)[PowerShell](/tag/powershell/)[Python](/tag/python/)[YAML](/tag/yaml/)

Table of Contents

*   [Prerequisites](#h-prerequisites)
*   [Using Python to Convert YAML to JSON](#h-using-python-to-convert-yaml-to-json)
*   [Adding YAML Support to Python](#h-adding-yaml-support-to-python)
*   [Writing the Conversion Script](#h-writing-the-conversion-script)
*   [Running the Conversion Script](#h-running-the-conversion-script)
*   [Using PowerShell to Convert YAML to JSON](#h-using-powershell-to-convert-yaml-to-json)
*   [Adding YAML Support to PowerShell](#h-adding-yaml-support-to-powershell)
*   [Writing the Conversion Script](#h-writing-the-conversion-script-1)
*   [Running the Conversion Script](#h-running-the-conversion-script-1)
*   [Using Go to Convert YAML to JSON](#h-using-go-to-convert-yaml-to-json)
*   [Adding YAML Support to Go](#h-adding-yaml-support-to-go)
*   [Writing the Conversion Script](#h-writing-the-conversion-script-2)
*   [Running the Conversion Script](#h-running-the-conversion-script-2)
*   [Conclusion](#h-conclusion)

Many modern tools, particularly in configuration management and Infrastructure as Code (IaC) spaces, use [JSON](https://www.w3schools.com/whatis/whatis_json.asp) or [YAML](https://www.w3schools.io/file/yaml-introduction/#:~:text=YAML%20stands%20for%20Ain't,It%20is%20case%20sensitive) 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.

Related:[PowerShell vs. Python: A Battle for the Ages](https://adamtheautomator.com/powershell-vs-python/)

## 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](https://code.visualstudio.com/) version 1.55.1. Feel free to use any code editor you want.

Related:[What You Need to Know about Visual Studio Code: A Tutorial](https://adamtheautomator.com/visual-studio-code-tutorial/)

*   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.

```yaml
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](https://www.python.org/downloads/) and [PIP](https://pypi.org/project/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](https://pypi.org/project/PyYAML/).

To install the PyYAML module, you’ll need to run the [`pip`](https://pip.pypa.io/en/stable/) 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.

```powershell
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](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-59-1.png)

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.

```python
## 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](http://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](http://convert-os.py)_ script you created in the previous section.

```powershell
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](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-60-1.png)

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](http://convert-os.py)_ Python script should have created. In this example, to open the file in notepad, run the command below in PowerShell.

```powershell
notepad C:\temp\python_operating-systems.json
```

![Viewing the python\_operating-systems.json file in notepad](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-61-1.png)

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](https://www.powershellgallery.com/packages/powershell-yaml/0.4.2), 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`](https://docs.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershell-7.1) cmdlet to install the PowerShell-yaml module.

```powershell
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](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_hash_tables?view=powershell-7.1).

`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.

```powershell
Get-Command -Module PowerShell-yaml
```

As you can see below, the [`Get-Command`](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/get-command?view=powershell-7.1) cmdlet listed the cmdlets from the PowerShell-yaml module.

![Getting the PowerShell-yaml cmdlets](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-62-1.png)

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._

```powershell
#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)
```

Related:[Get-Content: Reading Text Files like a Boss in PowerShell](https://adamtheautomator.com/powershell-get-content/)

Related:[Set-Content: The PowerShell Way to Write to a File](https://adamtheautomator.com/powershell-write-to-file/)

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.

```powershell
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.

```powershell
notepad C:\temp\PowerShell_operating-systems.json
```

![Viewing the content of the PowerShell\_operating-systems.json file](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-64-1.png)

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](https://golang.org/). The latest version as of this writing is go1.16.3. If you don’t have Go yet, refer to the [download and install](https://golang.org/doc/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](https://pkg.go.dev/gopkg.in/yaml.v3) package first using the [`go get`](https://golang.org/pkg/cmd/go/internal/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](https://gopkg.in/yaml.v3)_.

```powershell
go get gopkg.in/yaml.v3
```

![Installing the yaml.v3 package in Go](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-65-1.png)

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_.

```go
// 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`](https://golang.org/pkg/cmd/go/internal/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
go run c:\temp\convert-os.go
```

![Running Convert-os Go script.](https://adamtheautomator.com/wp-content/uploads/2021/04/convert-os_go.png)

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](https://adamtheautomator.com/wp-content/uploads/2021/04/operating-systems_json_go.png)

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?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fyaml-to-json%2F&text=How%20to%20Convert%20YAML%20to%20JSON%20%5BPython%2C%20PowerShell%2C%20Go%5D)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fyaml-to-json%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fyaml-to-json%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2020/12/Wrangling-REST-APIs-and-JSON-with-PowerShell.jpg)

### [PowerShell and JSON: A Comprehensive Guide](/powershell-json/)

Learn how to use PowerShell and JSON together, including converting and parsing JSON, with real-world examples.

![](https://adamtheautomator.com/wp-content/uploads/2019/11/box-62867_1280.jpg)

### [PowerShell vs. Python: A Battle for the Ages](/powershell-vs-python/)

Get a glimpse of PowerShell and Python and understand how they compare and contrast in features such as syntax, availability across platforms, and more.

![](https://adamtheautomator.com/wp-content/uploads/2019/07/connection-27185_1280.png)

### [Python WinRM: PowerShell Remoting for Windows](/python-winrm/)

Learn how to set up Python WinRM for PowerShell remoting on Windows machines in this comprehensive guide.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
