9

So I am having a bit of a problem here, I can't seem to figure out how to update data values in an Object

so lets say for example the FOLLOWING 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"
                    ]
                }
            }
        ]
    }
}

I have Powershell read it smoothly with convertfrom-json

How would I do the following:

A) Change "Oranges" from "2" to "100"

B) "Baskets" in Room 2 from "false" to "true"

C) add "bubblegum" to "candies" in Room1

HOW can I update this without rewriting the WHOLE json or object?

JiBéDoublevé
  • 4,124
  • 4
  • 36
  • 57
AdilZ
  • 1,107
  • 6
  • 27
  • 44

1 Answers1

12

The JSON becomes a custom object with nested objects, so really it is fairly simple. To start, let's fix the JSON by adding a comma after the Apples value, and convert it to an 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"                                                                    
                                                                ]
                                                }
                            }
                        ]
          }
}
'@ | ConvertFrom-JSON

Then if we want to update Oranges from 2 to 100 we simply change the value:

$JSON.Fruits.Oranges = 100

Similarly we can modify the Room 2 by simply listing the Places, passing it to a Where statement to get the right room, and modify the value in a ForEach loop.

$JSON.Places.Places | Where{$_.name -eq 'Room 2'} | ForEach{$_.Baskets = 'true'}

Lastly, since candies is defined as an array in the JSON we can simply add the desired candy to the array.

$JSON.Places.Places | Where{$_.name -eq 'Room 1'} | ForEach{$_.CandyTypesPresent.candies += 'bubblegum'}
Crono
  • 10,211
  • 6
  • 43
  • 75
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • AAAAAAH it was THAT SIMPLE... just a piped `foreach` !!! I was seriously narrow-mindedly looking for and also attempting to over-engineer this.... Thank you – AdilZ Jul 13 '17 at 02:05
  • but as i am setting content it is breaking my json structure. – Ashish-BeJovial Oct 30 '19 at 07:25
  • @themadtechnician how would you add a new object to places: $JSON.Places.Places[0].Color – Barry MSIH Mar 03 '20 at 18:11
  • It is easiest if you have the object that you want to add to it first, such as `$Color = 'Red'` or `$Color = 'Red','Blue','Yellow'`. Then you use `Add-Member` like: `Add-Member -InputObject $JSON.Places.Places[0] -Notepropertyname 'Color' -Notepropertyvalue $Color` – TheMadTechnician Mar 03 '20 at 19:46