【问题标题】:Powershell - create a json filePowershell - 创建一个 json 文件
【发布时间】:2021-01-28 15:37:20
【问题描述】:

我已经准备好Cloud Formation模板,json格式,如下:

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

                        }
                    ]
                }
            }
        }
    }
}

我需要将内容注入到那个 json 文件中,所以准备了代码。我需要注入两个数组:Values 和 Key

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, Audi",
                    "Key": "Standard"
                  },
                  {
                    "Values": "Fiat, Ferrari, Porsche, Toyota",
                    "Key": "High"
                  },
                  {
                    "Values": "*",
                    "Key": "Premium"
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

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

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

【问题讨论】:

  • 专业提示:您应该始终在函数中使用适当的缩进,这将使您的代码对您和其他人更具可读性和可维护性。

标签: json powershell amazon-cloudformation


【解决方案1】:

这是因为您的 $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", "Ferrari", "Porsche", "Toyota"
    Premium = "*"
}
$objecttoinjectArray = $hashtable.GetEnumerator() | foreach {
    [pscustomobject] @{
        Values = @($_.Value)
        Key = $_.Key
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-18
    • 2015-07-05
    • 1970-01-01
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多