I have prepared Cloud Formation template, json format, below:
{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
"Baseline": {
"Properties": {
"ApprovalRules": {
"PatchRules": [
{
"PatchFilterGroup": {
"PatchFilters": [
{
"Values": [],
"Key": ""
},
{
"Values": [],
"Key": ""
},
{
"Values": [],
"Key": ""
}
]
}
}
]
}
}
}
}
}
I need to inject content to that json file, so prepared code. I need to inject two arrays: Values and 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
And as an output I can see incorrectly formatted json file, below:
{
"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"
}
]
}
}
]
}
}
}
}
}
Proper structure should looks like presented below, that structure is not accepted by 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"
}
]
}
}
]
}
}
}
}
}