首页 > 解决方案 > 使用 PUT 方法更新 JSON 中的值

问题描述

我有一个嵌套的 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"
                }
            }
        }

我可以更改“名称”和“资产标签”,但问题是custom_fields → Mediensatz → 价值中的“价值” 。

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

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

标签: jsonpowershellnestedput

解决方案


推荐阅读