使用PUT方法更新JSON中的值

时间:2019-07-19 08:48:01

标签: json powershell nested put

我有一个嵌套的JSON文件。我想用PUT方法更新一些值。我可以更改一些值,但是问题出在嵌套值中。

$Authorization = "Bearer API-KEY"

$update = [ordered]@{
    name       = 'ppp'
    asset_tag  = 'BBB'
    custom_fields = @(
        [ordered] @{
            Mediensatz = @(
                [ordered] @{
                    value = "B2T-XXX"
                }
            )
        } 
    )
}
$json = $update | ConvertTo-Json -Depth 100

#Write-Host $json
$response = Invoke-RestMethod 'URL' -Method Put -Body $json -ContentType 'application/json' -Headers @{'Authorization' = $Authorization}

我的JSON文件如下:

{
    "total": 888,
    "rows": [
        {
            "id": 11,
            "name": "AAA",
            "asset_tag": "CCC",
            "model": {
                "id": 34,
                "name": "TTT"
            },
            "user_can_checkout": true,
            "custom_fields": {
                "Mediensatz": {
                    "field": "lll",
                    "value": "B2T-YYY",
                    "field_format": "ANY"
                },
                "Ueberschreibschutz": {
                    "field": "lll",
                    "value": "2019-07-10",
                    "field_format": "DATE"
                }
            }
        }

我可以更改“名称”和“ asset_tag”,但是问题是 custom_fields→Mediensatz→value 中的“ value”。

当我尝试使用Write-Host $json运行代码时,看起来像这样:

{
    "name": "PPP",
    "asset_tag": "BBB",
    "custom_fields": [
        {
            "Mediensatz": [
                 {
                     "value":  "B2T-XXX"
                 }
             ]
        }
    ]
}

0 个答案:

没有答案