1

So, I have my xml like shown below with Parameter 4 being an integer and the rest being string parameters. The problem is that XML doesn't allow me to put the integer value without quotes (it doesn't like that).

When I convert it to JSON, I want the integer value to come over without the quotes.

<?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="42" />
  </Parameters>
</Application>

I have a nested hash table (Thanks @mklement0 for assisting me with that)

    $hash = [ordered] @{}
    $appParametersXml.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 |Out-File $parameterJsonFile

The output in the JSON file is correct, however, the last value (42) is also coming over within quotes. Is there a way to specify that one specific value is an integer?

Thank you in advance for your help :)

1
  • 1
    The XML specification is pretty clear about this: Only string literals and references can be used as attribute values, and they must be delimited using single- or double-quotes. If you want to convert it to JSON as an integer value, you'll have to explicitly convert it to one before calling ConvertTo-Json Commented Oct 22, 2020 at 15:20

1 Answer 1

1

XML is text-based, so any values you extract from an [xml] (System.Xml.XmlDocument) document will be strings.

To quote from Mathias R. Jessen's comment on the question: "The XML specification is pretty clear about this: Only string literals and references can be used as attribute values, and they must be delimited using single- or double-quotes."

If you want those values to be other types, you must perform explicit conversions.

Passing such converted values to ConvertTo-Json will then automatically omit the quotes if they are of a type that is represented unquoted in JSON (numbers, null, true and false)

In the case at hand, you can use -as, the conditional type conversion operator, to conditionally convert a string to an integer, if it can be interpreted as such:

# Sample XML input.
# Note the two "Value" attribute values, "test1" and "42".
[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="parameter4" Value="42" />
  </Parameters>
</Application>
'@ 
  
# Transform the "Parameter" elements into a nested hashtable.
# Convert any values that can be interpreted as [int] to [int].
$hash = [ordered] @{}
$xmlDoc.Application.Parameters.ChildNodes | ForEach-Object {
  $hash[$_.Name] = @{
    # Convert the text value to an [int], if it can be parsed as such.
    value = if ($num = $_.Value -as [int]) { $num } else { $_.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 the following: note how 42 is unquoted:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "Parameter1": {
      "value": "test1"
    },
    "parameter4": {
      "value": 42
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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.