Powershell-创建一个json文件

问题描述

我准备了以下json格式的Cloud Formation模板:

{
"AWstemplateFormatVersion": "2010-09-09","Resources": {
        "Baseline": {
            "Properties": {
            
                "ApprovalRules": {
                    "PatchRules": [
                        {
                            "PatchFilterGroup": {
                                "PatchFilters": [
                                    {
                                        "Values": [],"Key": ""
                                    },{
                                        "Values": [],"Key": ""
                                    }
                                ]
                            }

                        }
                    ]
                }
            }
        }
    }
}

我需要将内容注入该json文件,因此需要准备代码。我需要注入两个数组:ValuesKey

function ObjectBuilder {

    param (
    $Values,$Key
    )

    $counter = 0
    $objecttoinjectArray = @()
    foreach ($element1 in $Values) { 
            $objecttoinject = [pscustomobject][ordered] @{
                            Values = $element1
                            Key = $Key[$counter]
            }
            $objecttoinjectArray += $objecttoinject
            $counter++
        
    }
    return $objecttoinjectArray

}


$filename = "Matrix.json"
$content = Get-Content -Path .\$filename
$jsoncontent = $content | ConvertFrom-Json
$jsonbuild = $jsoncontent.Resources.Baseline.Properties
$Values = @("BMW,Audi","Fiat,Ferrari,Porsche,Toyota","*")
$Key = @("Standard","High","Premium")

$objecttoinjectArray = ObjectBuilder -Values $Values -Key $Key
$jsonbuild.ApprovalRules.PatchRules.PatchFilterGroup.PatchFilters = $objecttoinjectArray

$jsoncontent | 
ConvertTo-Json -Depth 15 | 
Set-Content .\tests.json

作为输出,我可以在下面看到格式错误的json文件

{
  "AWstemplateFormatVersion": "2010-09-09","Resources": {
    "Baseline": {
      "Properties": {
        "ApprovalRules": {
          "PatchRules": [
            {
              "PatchFilterGroup": {
                "PatchFilters": [
                  {
                    "Values": "BMW,"Key": "Standard"
                  },{
                    "Values": "Fiat,"Key": "High"
                  },{
                    "Values": "*","Key": "Premium"
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

正确的结构应如下所示,该结构未被AWS cloudformation接受:

{
  "AWstemplateFormatVersion": "2010-09-09","Resources": {
    "Baseline": {
      "Properties": {
        "ApprovalRules": {
          "PatchRules": [
            {
              "PatchFilterGroup": {
                "PatchFilters": [
                  {
                    "Values": [
            "BMW","Audi"
            ],{
                    "Values": [
            "Fiat","Ferrari","Porsche","Toyota"
            ],{
                    "Values": [
            "*"
            ],"Key": "Premium"
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

解决方法

这是因为$Values的每一项都是一个字符串,而不是一个数组("BMW,Audi"而不是"BMW","Audi"),因此将被添加到JSON中。

根据从何处获取这些值,有几种选择:

如果字符串来自文件,则必须将其拆分:

$objecttoinject = [pscustomobject]@{
    # split up the string into an actual array of the values
    Values = $element1 -split ","
    Key = $Key[$counter]
}

(顺便说一句,您可以省略[ordered]加速器,因为[pscustomobject]已经暗示了它)

如果它们确实是在脚本中实际定义的,也可以对其进行更改:

$Values = @(@("BMW","Audi"),@("Fiat","Ferrari","Porsche","Toyota"),@("*"))

或更妙的是,使用哈希表:

$hashtable = @{
    Standard = "BMW","Audi"
    High = "Fiat","Toyota"
    Premium = "*"
}
$objecttoinjectArray = $hashtable.GetEnumerator() | foreach {
    [pscustomobject] @{
        Values = @($_.Value)
        Key = $_.Key
    }
}