Convert XML to JSON in PowerShell

PowerShell offers various cmdlets to work with JSON and XML formats. This guide will focus on converting an XML document to JSON formatted string using PowerShell.
Access XML Document Keys and Values
PowerShell enables manipulating XML document nodes using Select-Xml cmdlet. We can specify XPath expressions to find nodes and their text values in an XML document.
Syntax:
Select-Xml
[-XPath] <string>
[-Path] <string[]>
[-Namespace <hashtable>]
[<CommonParameters>]
Let’s create an XML document called employeeinfo.xml
with the following content.
XML - employeeinfo.xml
:
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee>
<EmpId>1</EmpId>
<EmpAge>45</EmpAge>
<EmpDesignation>SSE</EmpDesignation>
</Employee>
<Employee>
<EmpId>2</EmpId>
<EmpAge>34</EmpAge>
<EmpDesignation>Junior SE</EmpDesignation>
</Employee>
</Employees>
Now, we are going to access each XML node EmpId
, EmpAge
, and EmpDesignation
using the Select-Xml
cmdlet as follows.
Code:
$empIds = Select-Xml -Path "D:\codes\employeeinfo.xml" -XPath "//Employee//EmpId" | foreach {$_.node.InnerText}
$empAges = Select-Xml -Path "D:\codes\employeeinfo.xml" -XPath "//Employee//EmpAge" | foreach {$_.node.InnerText}
$empDesigs = Select-Xml -Path "D:\codes\employeeinfo.xml" -XPath "//Employee//EmpDesignation" | foreach {$_.node.InnerText}
In this example, the -Path
is the location where the employeeinfo.xml
is located. Since we need to fetch three nodes per employee object, PowerShell foreach
has been used.
The XPath expression looks like the following for each node.
EmpID
node can be accessed by the XPath query "//Employee//EmpId"
.
EmpAge
node can be accessed by the XPath query "//Employee//EmpIAge"
.
EmpDesignation
node can be accessed by the XPath query "//Employee//EmpDesignation"
.
The reference variables $empIds
, $empAges
and $empDesigs
contains array of values for respective XML nodes. Let’s write the arrays into PowerShell console windows.
Construct PowerShell Hash to Hold Data
We do not have a straightforward approach to converting an XML document to a JSON string. Hence, as shown in the above section, we have to extract XML data and push those data to a PowerShell hash as an intermediate format.
Let’s construct the hash.
$empObjHash1 = @{
EmpId = $empIds[0];
EmpAge = $empAges[0];
EmpDesignation = $empDesigs[0];
}
$empObjHash2 = @{
EmpId = $empIds[1];
EmpAge = $empAges[1];
EmpDesignation = $empDesigs[1];
}
$finalHash = @{}
$finalHash.Add("emp1", $empObjHash1)
$finalHash.Add("emp2", $empObjHash2)
We have extracted the two employee objects from the XML for demonstration purposes and pushed those data to two hash objects, $empObjHash1
and $empObjHash2
. Then the $finalHash
is created by pushing the two employee objects.
If we print the $finalHash
, it would look like the following.
We can use the foreach
loop to inspect the $finalHash
in more detail.
Code:
foreach($em in $finalHash.keys) {
foreach($emp in $finalHash[$em]) {
foreach($val in $emp.keys) {
Write-Host "Key:" $val "Value:" $emp[$val]
}
}
}
Output:
The $finalHash
has been constructed with the relevant data extracted from the XML.
Convert Hash to JSON
Since we got a PowerShell hash $finalHash
, it is possible to use the ConvertTo-Json
cmdlet to create a JSON out of the hash.
Syntax:
ConvertTo-Json
[-InputObject] <Object>
[-Depth <Int32>]
[-Compress]
[-EnumsAsStrings]
[-AsArray]
[-EscapeHandling <StringEscapeHandling>]
[<CommonParameters>]
Let’s pass the created hash $finalHash
to create the JSON string as shown in the following.
Code:
$finalHash | ConvertTo-Json
Output:
PowerShell Full Script:
$empIds = Select-Xml -Path "D:\codes\employeeinfo.xml" -XPath "//Employee//EmpId" | foreach {$_.node.InnerText}
$empAges = Select-Xml -Path "D:\codes\employeeinfo.xml" -XPath "//Employee//EmpAge" | foreach {$_.node.InnerText}
$empDesigs = Select-Xml -Path "D:\codes\employeeinfo.xml" -XPath "//Employee//EmpDesignation" | foreach {$_.node.InnerText}
$empObjHash1 = @{
EmpId = $empIds[0];
EmpAge = $empAges[0];
EmpDesignation = $empDesigs[0];
}
$empObjHash2 = @{
EmpId = $empIds[1];
EmpAge = $empAges[1];
EmpDesignation = $empDesigs[1];
}
$finalHash = @{}
$finalHash.Add("emp1", $empObjHash1)
$finalHash.Add("emp2", $empObjHash2)
foreach($em in $finalHash.keys) {
foreach($emp in $finalHash[$em]) {
foreach($val in $emp.keys) {
Write-Host "Key:" $val "Value:" $emp[$val]
}
}
}
$finalHash | ConvertTo-Json
Nimesha is a Full-stack Software Engineer for more than five years, he loves technology, as technology has the power to solve our many problems within just a minute. He have been contributing to various projects over the last 5+ years and working with almost all the so-called 03 tiers(DB, M-Tier, and Client). Recently, he has started working with DevOps technologies such as Azure administration, Kubernetes, Terraform automation, and Bash scripting as well.