【问题标题】:How to add additional elements in an existing json file如何在现有 json 文件中添加其他元素
【发布时间】:2021-02-16 07:21:04
【问题描述】:

这就是我正在做的:

$appParametersXml = [Xml] (Get-Content "$appParameterFilePath\$appParameterFile") 
    $parameterJsonFile = "$appParameterFilePath\$applicationName"+ "." + $jsonFileName

    # Transform the "Parameter" elements into a nested hashtable.
    # Convert any values that can be interpreted as [int] to [int] and strip out any comments in the xml file.
    $hash = [ordered] @{}
    $appParametersXml.Application.Parameters.ChildNodes | Where-Object {$_.NodeType -ne 'Comment'} | % {
    $hash[$_.Name] = @{ 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 |Out-File $parameterJsonFile
     Write-Host "The JSON File is: " $parameterJsonFile

在使用 XML 文件中的现有信息构建哈希表后,我需要在转换为 JSON 之前添加类似这样的附加参数值

"parameters":  {
               "applicationName":  {
                        "value":  "somevalue"
                                           },
               "applicationTypeName":  {
                         "value":  "somevalue"
                                               },
                "applicationTypeVersion":  {
                          "value":  "somevalue"
                  },

到目前为止,我所尝试的一切都赋予了我额外的价值。常规 XML 值正在以正确的方式进行转换,但我在转换之前添加的其他项目是这样出现的!

"applicationName":  "somevalue"

我怎样才能把它分成不同的行?

【问题讨论】:

  • 所以你不想得到别人的帮助?
  • :)。完全不是我说的!我试图标记某人,但我失败了。感谢大家的专业知识
  • 好吧,我不会撒谎,他的帮助是最好的帮助之一。 :)
  • 您可以直接编辑原始 xml(使用 xml 工具),使其成为最终形状,然后进行转换吗?还是根本不转换,只在修改后的xml上继续使用xml工具?

标签: json xml powershell


【解决方案1】:

所以,假设您的输入 xml 文件看起来像这样......

<application>
    <parameters>
        <applicationName>My Awesome App</applicationName>
        <!--my awesome comment-->
        <applicationTypeName>Mobile App</applicationTypeName>
        <applicationTypeVersion>299</applicationTypeVersion>
        <!--my other awesome comment-->
    </parameters>
</application>

这是我修改后的 PowerShell ...您不能使用 if ($num = $_.Value -as [int]) 强制转换,因为它不适用于 0,因为它会被解释为错误。我更喜欢分解步骤并测试和检查每个步骤。此外,我使用InnerText 作为节点值而不是Value,因为通常Valueevaluated as $null,我不确定您的xml 是什么样的。

$fileXml = "./config.xml"
$fileJson = "./config.json"

$xmlContent = [Xml](Get-Content $fileXml)

# Transform the "Parameter" elements into a nested hashtable.
# Set any string values which are integers as [int] and strip out any comments in the xml file.
$parameters = [ordered]@{}
$nodes = $xmlContent.application.parameters.ChildNodes.Where{ $_.NodeType -ne 'Comment' }

foreach ($node in $nodes) {
    $parameter = $node.Name
    $value = $node.InnerText
    if ($value -match "^\d+$") { $value = [int] $value }
    $parameters.Add($parameter, @{ value = $value })
}

# if you need to add additional attributes, it's as simple as:

$parameters.Add("newParameter1", @{ value = "newValue1" })
$parameters.Add("newParameter2", @{ value = "newValue2" })

# 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     = $parameters
} | ConvertTo-Json | Out-File $fileJson

这是保存到 json 文件的输出:

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "applicationName": {
      "value": "My Awesome App"
    },
    "applicationTypeName": {
      "value": "Mobile App"
    },
    "applicationTypeVersion": {
      "value": 299
    },
    "newParameter1": {
      "value": "newValue1"
    },
    "newParameter2": {
      "value": "newValue2"
    }
  }
}

【讨论】:

  • 谢谢!这很适合将常规 XML 文件转换为 JSON。如果我需要在转换为 JSON 之前将其他 Parameter 元素添加到哈希表中,该怎么办。所以我想要常规的 XML 文件,再加上一些参数(不在原始 XML 文件中),然后将其全部转换为 JSON>
  • 对不起,我认为我在上面的问题中不清楚。我已经编辑了问题并添加了更多信息
  • @Amy - 我已经更新了答案以提供所需的步骤。在您最初的问题中,您专门询问嵌套 value 属性。如果您能将我标记为答案,将不胜感激。干杯!
【解决方案2】:

万一其他人遇到这种情况,只需在使用现有 XML 文件创建哈希表后执行此操作即可


$appParametersFileName = "$appParameterFilePath\$appParameterFile"
            
    $appParametersXml = [Xml] (Get-Content "$appParametersFileName") 
    $parameterJsonFile = "$appParameterFilePath\$applicationName"+ "." + $jsonFileName
    # Transform the "Parameter" elements into a nested hashtable.
    # Convert any values that can be interpreted as [int] to [int] and strip out any comments in the xml file.
    $hash = [ordered] @{}
    $appParametersXml.Application.Parameters.ChildNodes | Where-Object {$_.NodeType -ne 'Comment'} | % {
    $hash[$_.Name] = @{ value = if ($num = $_.Value -as [int]) { $num } else { $_.Value }
       }
    }
    $hash["newvalue1"]=@{ value="value1"}
    $hash["newvalue2"]=@{ value="value2"}
    
    # 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
     Write-Host "The JSON File is: " $parameterJsonFile

【讨论】:

    猜你喜欢
    • 2018-09-28
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 2016-04-05
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    相关资源
    最近更新 更多