Powershell:如何在Json和XML Object中更新/替换数据和值

Adi*_*ilZ 4 powershell json object updates

所以我在这里遇到了一些问题,我似乎无法弄清楚如何更新Object中的数据值

让我们举个例如关注的json

{
    "People": 263,
    "Hungry": true,
    "Fruits": {
        "Apples": 1 "Oranges": 2
    },
    "Places": {
        "Places": [{
                "Baskets": "true",
                "name": "Room 1",
                "candycount": 1500,
                "candytypespresent": {
                    "candies": [
                        "caramel"
                    ]
                }

            },
            {

                "Baskets": "false",
                "name": "Room 2",
                "candycount": 2000,
                "candytypespresent": {
                    "candies": [
                        "caramel",
                        "jawbreaker",
                        "butterscotch"
                    ]
                }
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

我有Powershell顺利阅读它 convertfrom-json

我该怎么做:

A)将"橘子"从"2"改为"100"

B)房间2中的"篮子"从"假"到"真"

C)在Room1中将"bubblegum"添加到"糖果"中

如何在不重写WHOLE json或对象的情况下更新此内容

The*_*ian 6

JSON成为具有嵌套对象的自定义对象,所以实际上它非常简单.首先,让我们通过在Apples值之后添加逗号来修复JSON,并将其转换为对象...

$JSON = @'
{
"People":  263,
"Hungry":  true,
"Fruits":  {
                "Apples":  1,
                "Oranges":  2
            },
"Places":  {
              "Places":  [
                            {
                                "Baskets":  "true",
                                "name":  "Room 1",
                                "candycount":  1500,
                                "candytypespresent":  {
                                                     "candies":  [
                                                                     "caramel"
                                                                 ]
                                                 }

                            },
                            {

                                "Baskets":  "false",
                                "name":  "Room 2",
                                "candycount":  2000,
                                "candytypespresent":  {
                                                     "candies":  [
                                                                    "caramel",
                                                                    "jawbreaker",
                                                                    "butterscotch"                                                                    
                                                                ]
                                                }
                            }
                        ]
          }
}
'@ | ConvertFrom-JSON
Run Code Online (Sandbox Code Playgroud)

然后,如果我们想要将Oranges从2更新为100,我们只需更改值:

$JSON.Fruits.Oragnes = 100
Run Code Online (Sandbox Code Playgroud)

类似地,我们可以通过简单地列出Places来修改Room 2,将它传递给Where语句以获得正确的空间,并在ForEach循环中修改值.

$JSON.Places.Places | Where{$_.name -eq 'Room 2'} | ForEach{$_.Baskets = 'true'}
Run Code Online (Sandbox Code Playgroud)

最后,由于candies在JSON中定义为数组,我们可以简单地将所需的糖果添加到数组中.

$JSON.Places.Places | Where{$_.name -eq 'Room 1'} | ForEach{$_.CandyTypesPresent.candies += 'bubblegum'}
Run Code Online (Sandbox Code Playgroud)