Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell-将内容注入json文件_Json_Powershell_Amazon Cloudformation - Fatal编程技术网

Powershell-将内容注入json文件

Powershell-将内容注入json文件,json,powershell,amazon-cloudformation,Json,Powershell,Amazon Cloudformation,我必须向json文件中注入一些数据。我使用的是powershell代码,如下所示。这只是我脚本的摘录。变量$MwTagsSelected是pscustom对象的数组,如下所示。 $MwTagsSelected有三个属性:TagIndex,TagName,TagValue。我将该对象传递给下面显示的foreach循环,结果得到对象数组$FilteredObjectArray。两个对象数组如下所示。我想使用code$jsoncontent.Resources.MaintenanceWindowTar

我必须向json文件中注入一些数据。我使用的是powershell代码,如下所示。这只是我脚本的摘录。变量
$MwTagsSelected
是pscustom对象的数组,如下所示。
$MwTagsSelected
有三个属性:
TagIndex
TagName
TagValue
。我将该对象传递给下面显示的
foreach
循环,结果得到对象数组
$FilteredObjectArray
。两个对象数组如下所示。我想使用code
$jsoncontent.Resources.MaintenanceWindowTarget.Properties.Targets=$FilteredObjectArray
$FilteredObjectArray
注入json文件

$Mwtag已选择:

$FilteredObjectArray

我的powershell代码:

# get JSON file content
$filename = "inputfile.json"
$content = Get-Content -Path .\$filename
$jsoncontent = $content | ConvertFrom-Json
# JSON file input preparation
$Keys = ($MwTagsSelected | Select-Object -Property TagName -unique).TagName
$FilteredObjectArray = @()
$NotAllowedSelections = @()
$Value = @()

foreach ($Key in $Keys) {
        $Value += $MwTagsSelected | Where-Object -FilterScript {$_.TagName -eq $Key}
        $FilteredObject = [pscustomobject][ordered] @{
            Key = "tag:$Key"
            Values = $Value.TagValue
        }

    if ($Value.Count -gt 5) {
        $NotAllowedSelections += $Key
    }
    $FilteredObjectArray += $FilteredObject
    $Value = @()
}

$jsoncontent.Resources.MaintenanceWindowTarget.Properties.Targets = $FilteredObjectArray

$jsoncontent | 
ConvertTo-Json -Depth 15 | 
Set-Content .\test.json
作为该脚本的输出,我得到了json文件,但json结构不是预期的,下面是从我的output test.json文件中提取的内容

"Targets": [
          {
            "Key": "tag:win",
            "Values": [
              "01",
              "02"
            ]
          },
          {
            "Key": "tag:ein",
            "Values": "03"
          }
        ],
Output test.json文件应如下所示:

"Targets": [
          {
            "Key": "tag:win",
            "Values": [
              "01",
              "02"
            ]
          },
          {
            "Key": "tag:ein",
            "Values": [
              "03"
            ]
          }
        ],

如果要确保值是一个数组,可以用
@()

参见这些简化示例

样本数据

$ht = @{
    Tag="win"
    TagValue=1,2
},
@{
    Tag="ein"
    TagValue=3
}
没有
@()

$ht.GetEnumerator() | % {
    [PSCustomObject]@{
        Key = $_.tag
        Values = $_.tagvalue
    }
}  | ConvertTo-Json

[
    {
        "Key":  "win",
        "Values":  [
                       1,
                       2
                   ]
    },
    {
        "Key":  "ein",
        "Values":  3
    }
]
$ht.GetEnumerator() | % {
    [PSCustomObject]@{
        Key = $_.tag
        Values = @($_.tagvalue)
    }
}  | ConvertTo-Json

[
    {
        "Key":  "win",
        "Values":  [
                       1,
                       2
                   ]
    },
    {
        "Key":  "ein",
        "Values":  [
                       3
                   ]
    }
]
@()

$ht.GetEnumerator() | % {
    [PSCustomObject]@{
        Key = $_.tag
        Values = $_.tagvalue
    }
}  | ConvertTo-Json

[
    {
        "Key":  "win",
        "Values":  [
                       1,
                       2
                   ]
    },
    {
        "Key":  "ein",
        "Values":  3
    }
]
$ht.GetEnumerator() | % {
    [PSCustomObject]@{
        Key = $_.tag
        Values = @($_.tagvalue)
    }
}  | ConvertTo-Json

[
    {
        "Key":  "win",
        "Values":  [
                       1,
                       2
                   ]
    },
    {
        "Key":  "ein",
        "Values":  [
                       3
                   ]
    }
]

另外,您不需要将
[Ordered]
[PSCustomObject]
-默认情况下,它将保留顺序。

如果要确保值是一个数组,可以用
@()

参见这些简化示例

样本数据

$ht = @{
    Tag="win"
    TagValue=1,2
},
@{
    Tag="ein"
    TagValue=3
}
没有
@()

$ht.GetEnumerator() | % {
    [PSCustomObject]@{
        Key = $_.tag
        Values = $_.tagvalue
    }
}  | ConvertTo-Json

[
    {
        "Key":  "win",
        "Values":  [
                       1,
                       2
                   ]
    },
    {
        "Key":  "ein",
        "Values":  3
    }
]
$ht.GetEnumerator() | % {
    [PSCustomObject]@{
        Key = $_.tag
        Values = @($_.tagvalue)
    }
}  | ConvertTo-Json

[
    {
        "Key":  "win",
        "Values":  [
                       1,
                       2
                   ]
    },
    {
        "Key":  "ein",
        "Values":  [
                       3
                   ]
    }
]
@()

$ht.GetEnumerator() | % {
    [PSCustomObject]@{
        Key = $_.tag
        Values = $_.tagvalue
    }
}  | ConvertTo-Json

[
    {
        "Key":  "win",
        "Values":  [
                       1,
                       2
                   ]
    },
    {
        "Key":  "ein",
        "Values":  3
    }
]
$ht.GetEnumerator() | % {
    [PSCustomObject]@{
        Key = $_.tag
        Values = @($_.tagvalue)
    }
}  | ConvertTo-Json

[
    {
        "Key":  "win",
        "Values":  [
                       1,
                       2
                   ]
    },
    {
        "Key":  "ein",
        "Values":  [
                       3
                   ]
    }
]

另外,您不需要将
[Ordered]
[PSCustomObject]
-默认情况下它将保留订单。

您好,是的,它可以工作,我刚刚测试了解决方案,谢谢!!嗨,是的,它工作了,我刚刚测试了解决方案,谢谢!!