1

I have an XML file that looks like this

<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="application" xmlns="http://schemas.microsoft.com/2011/01/fabric">
  <Parameters>
    <Parameter Name="Parameter1" Value="test1" />
    <Parameter Name="parameter2" Value="test2" />
    <Parameter Name="parameter3" Value="test3" />
    <Parameter Name="parameter4" Value="test4" />
  </Parameters>
</Application>

I am using a hashtable to convert this to JSON but when I do, I only get the Parameters portion:

{
    "PARAMETER_1":  "Test",
    "parameter2" :  "test2" 

I'm Reading the xml file like so because if i try to read it as $appParametersXml then I get an error.

$appParametersHashTable = Read-XmlElementAsHashtable $appParametersXml.Application.Parameters

$appParametersHashTable |ConvertTo-Json |Out-File "C:\Workspaces\ARMTemplates\GhostDraftApplication\ARMTemplate\CloudAXF.json"
#$appParametersHashTable|ConvertFrom-Xml|Out-File "C:\Workspaces\ARMTemplates\GhostDraftApplication\ARMTemplate\CloudTest.xml"

$appParametersXml | ConvertFrom-Xml | ConvertTo-Json -Depth 4 | Out-File "C:\Workspaces\ARMTemplates\GhostDraftApplication\ARMTemplate\CloudAXF.json"

$xmlObject = $appParametersXml | ConvertTo-Xml


###############################################################################
function Read-XmlElementAsHashtable
{
    Param (
        [System.Xml.XmlElement]
        $Element
    )

    $hashtable = @{}
    if ($Element.ChildNodes)
    {
        $Element.ChildNodes | 
            ForEach-Object {
                    $hashtable["Name" + $_.Name] = "Value" + $_.Value
            }
    }

    return $hashtable
}

Is there a way to read the all the tags and attributes from the xml file and convert them to JSON so it looks like this?

"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
    "PARAMETER_1": {
        "value": "Test"
    },

I've just been spinning my wheels.

3
  • Sorry, my xml file didn't post correctly: It looks like this: <?xml version="1.0" encoding="utf-8"?> <Application xmlns:xsd="w3.org/2001/XMLSchema" xmlns:xsi="w3.org/2001/XMLSchema-instance" Name="Test" xmlns="schemas.microsoft.com/2011/01/fabric"> <Parameters> <Parameter Name="PARAMTER_1" Value="Test" /> Commented Oct 21, 2020 at 12:52
  • I fixed the code fences for you (it's three backticks, not three single-quotes), can you update the post with a complete and valid XML document please? Commented Oct 21, 2020 at 12:55
  • Thankyou! i've updated the post with the xml document Commented Oct 21, 2020 at 13:08

1 Answer 1

1

If you can hard-code what attributes to extract from the Parameter XML elements, you can do the following:

# Sample XML input.
[xml] $xmlDoc = @'
<?xml version="1.0" encoding="utf-8"?>
<Application xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" Name="application" xmlns="http://schemas.microsoft.com/2011/01/fabric">
  <Parameters>
    <Parameter Name="Parameter1" Value="test1" />
    <Parameter Name="parameter2" Value="test2" />
    <Parameter Name="parameter3" Value="test3" />
    <Parameter Name="parameter4" Value="test4" />
  </Parameters>
</Application>
'@ 
  
# Transform the "Parameter" elements into a nested hashtable.
$hash = [ordered] @{}
$xmlDoc.Application.Parameters.ChildNodes | % {
  $hash[$_.Name] = @{ value = $_.Value }
}

# Wrap the hashtable in a top-level hashtable and convert to JSON.
[ordered] @{
  '$schema' = 'https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#'
  contentVersion ='1.0.0.0'
  parameters = $hash
} | ConvertTo-Json

The above yields:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "Parameter1": {
      "value": "test1"
    },
    "parameter2": {
      "value": "test2"
    },
    "parameter3": {
      "value": "test3"
    },
    "parameter4": {
      "value": "test4"
    }
  }
}

Note: If you additionally want to perform type conversions on the XML input before converting to JSON, see this answer to a closely related question.

Sign up to request clarification or add additional context in comments.

1 Comment

I've added a link to your other question to the answer, in case people get here first while also looking for type conversions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.