JSON (JavaScript Object Notation) and XML (eXtensible Markup Language) are two widely used data formats in modern IT environments. However, we often need to convert JSON files to XML files using PowerShell.
This tutorial explains how to convert JSON to XML using PowerShell, and I will cover step-by-step instructions, practical examples, and common mistakes that you should avoid.
Why Convert JSON to XML?
JSON is lightweight, easy to read, and often used for APIs, web services, and configuration in modern applications. XML, on the other hand, remains the backbone for many enterprise systems and legacy software. Many organizations require converting JSON data sources into XML for compatibility or integration purposes.
Typical use cases for this conversion include:
- Migrating API data to XML-based systems
- Integrating cloud scripts with legacy applications
- Automating configuration updates across Windows servers
Understanding JSON and XML in PowerShell
PowerShell natively supports both JSON and XML handling.
- JSON is easily managed using the
ConvertFrom-JsonandConvertTo-Jsoncmdlets, which help import and export JSON data to and from PowerShell objects. - XML can be processed using the
[xml]type accelerator and theConvertTo-Xmlcmdlet, giving powerful tools for working with XML documents, nodes, and attributes.
Step 1: Import JSON Data
First, let’s read a JSON file or string into a PowerShell variable. PowerShell reads files using Get-Content, and you can convert JSON text to PowerShell objects with ConvertFrom-Json.
$jsonData = Get-Content -Path "data.json" -Raw | ConvertFrom-JsonIf you have the JSON as a string, you can convert it directly:
$jsonString = '{"name": "Alice", "role": "Admin"}'
$jsonData = $jsonString | ConvertFrom-JsonHere is the exact output you can see in the screenshot below:

At this stage, jsonData is a rich PowerShell object, making it easy to manipulate and access individual properties.
Step 2: Convert JSON to XML
PowerShell does not have a one-step native cmdlet for JSON-to-XML conversion. Instead, you convert JSON to a PowerShell object, then convert that object to XML using ConvertTo-Xml.
$jsonData = Get-Content -Path "data.json" -Raw | ConvertFrom-Json
$xmlData = ConvertTo-Xml -InputObject $jsonData -As String
$xmlData | Out-File "output.xml"ConvertTo-Xml -InputObject … -As Stringoutputs XML in string format, ideal for saving to a file.Out-File "output.xml"writes the XML to disk.
You can also display the XML in the console:
$xmlDataCheck out Convert JSON to CSV in PowerShell?
Step 3: Working with XML Data
Once your XML is generated, you can load and manipulate it using PowerShell’s [xml] type accelerator.
[xml]$xmlContent = Get-Content -Path "output.xml"
$xmlContent.DocumentElementThis makes it easy to traverse nodes, search for elements, and extract information.
For instance, to access an element property:
$xmlContent.DocumentElement.ChildNodes[0]Step 4: Handling Complex JSON Structures
Real-world JSON often includes nested arrays and objects. PowerShell handles these but may format them differently in XML. Here’s how to process a sample array within a JSON object:
$json = '{
"employees": [
{"name": "John", "role": "Admin"},
{"name": "Jane", "role": "Developer"}
]
}'
$data = $json | ConvertFrom-Json
$xml = ConvertTo-Xml -InputObject $data -As String
$xml | Out-File "employees.xml"You can see the exact output in the screenshot below:

PowerShell’s XML conversion will nest arrays as child nodes, preserving hierarchy. It’s important to inspect the resulting XML for structure conformity.
Read Convert String to JSON in PowerShell
Step 5: Save and Validate XML Output
After conversion, XML output can be easily saved, shared, or validated as needed. Saving is straightforward with Out-File, as shown above. For validation, use PowerShell’s built-in Select-Xml to check or query XML.
Select-Xml -Path "output.xml" -XPath "//Object[name='Alice']"Proper formatting can be ensured by reviewing indentation and custom root nodes with parameters in ConvertTo-Xml.
Step 6: Convert XML Back to JSON
It’s often necessary to reverse the process, converting XML to JSON. This is handy for modern applications needing JSON input.
[xml]$xmlData = Get-Content -Path "output.xml"
$json = $xmlData | ConvertTo-Json -Depth 10Use the -Depth parameter for nested structures, ensuring all levels are correctly captured.
Check out Write JSON to File in PowerShell
Best Practices and Tips
- Always test conversion with a small JSON sample before running large batch operations.
- Use the
-Depthparameter inConvertTo-Jsonfor complex objects. - Validate the generated XML against application requirements.
- Be aware that some data types (like booleans and nulls) may be represented differently after conversion.
- Preserve original data files in case manual corrections to XML are needed.
Common Errors and Troubleshooting
- “Cannot convert value” error: Check that the JSON is properly formatted and contains no undefined values.
- Empty XML elements sometimes occur due to nulls in JSON. Consider adding defaults or cleaning the JSON before conversion.
- When nested arrays misconvert, inspect with manual parsing or additional script logic.
If unexpected output formats appear, verify both the initial PowerShell object and its XML result.
Conclusion
I hope you understand how to convert JSON to XML format in PowerShell using the steps mentioned above. Do let me know if you have any questions for me in the comments below.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.