Skip to content

Instantly share code, notes, and snippets.

@elvarb
Created June 24, 2019 13:45
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save elvarb/a3e2f7b6ed5e56ae38c1c7e35d7016d9 to your computer and use it in GitHub Desktop.
Save elvarb/a3e2f7b6ed5e56ae38c1c7e35d7016d9 to your computer and use it in GitHub Desktop.
Powershell to convert XML to JSON
# From https://stackoverflow.com/questions/42636510/convert-multiple-xmls-to-json-list
# Use
# [xml]$var = Get-Content file.xml
# Convert to JSON with
# $var | ConvertFrom-XML | ConvertTo-JSON -Depth 3
# Helper function that converts a *simple* XML document to a nested hashtable
# with ordered keys.
function ConvertFrom-Xml {
param([parameter(Mandatory, ValueFromPipeline)] [System.Xml.XmlNode] $node)
process {
if ($node.DocumentElement) { $node = $node.DocumentElement }
$oht = [ordered] @{}
$name = $node.Name
if ($node.FirstChild -is [system.xml.xmltext]) {
$oht.$name = $node.FirstChild.InnerText
} else {
$oht.$name = New-Object System.Collections.ArrayList
foreach ($child in $node.ChildNodes) {
$null = $oht.$name.Add((ConvertFrom-Xml $child))
}
}
$oht
}
}
[xml[]] (Get-Content -Raw file[12].xml) | ConvertFrom-Xml | ConvertTo-Json -Depth 3
# -Depth might need tweaking depending on the depth of the XML file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment