1

I need to use Powershell to insert data into a json file within an array. Here is the json original structure

{
"Array": [
    {
        "servername": "foo"
    },
    {
        "servername": "boo"
    }
    ]
}

Here is the new structure:

   {
    "Array": [
        {
            "servername": "Foo",
            "Disk": "100"
        },
        {
            "servername": "Boo",
            "Disk": "500"
        }
    ]
   }

Why does this not work?

$json = @"
{
    "Array": [
        {
            "servername": "Foo"
        },
        {
            "servername": "Boo"
        }
        ]
    }
"@

$jsonServerFoo=@"
    {
    "Disk": "100"
    }
"@

$jsonServerBoo=@"
    {
    "Disk": "500"
    }
"@

$jsonObj = $JSON | ConvertFrom-Json
$jsonServerFooObj = $jsonServerFoo | ConvertFrom-Json
$jsonServerBooObj = $jsonServerBoo | ConvertFrom-Json
$i = 1 
$jsonObj.Array | Foreach-object {
    if ( $i -eq 1 ) {
        $_ | Add-Member -MemberType NoteProperty -Name "Disk" -Value $jsonServerFooObj.Disk
        $i++
    }
    else {
        $_ | Add-Member -MemberType NoteProperty -Name "Disk" -Value $jsonServerBooObj.Disk
    }
}
$jsonObj
$jsonObj | ConvertTo-Json -Depth 100

I get the desired output

{
    "Array":  [
                  {
                      "servername":  "Foo",
                      "Disk":  "100"
                  },
                  {
                      "servername":  "Boo",
                      "Disk":  "500"
                  }
              ]
}
2
  • 1
    With PS6 there is a “-AsHastable” property of Convert-FromJson which fixes this historic oversight.. Commented Dec 20, 2019 at 19:17
  • Why did you edit the solution into your question? Commented Dec 21, 2019 at 1:51

1 Answer 1

3

Use Add-Member to add members to a PSCustomObject. ConvertFrom-Json returns a PSCustomObject, not a Hashtable.

$jsonObj | Add-Member -MemberType NoteProperty -MemberName MyPropertyName -Value "some value, doesn't have to be a string"

You don't have to work with the base $jsonObj either, you can also add members to nested members of $jsonObj as well. In your case, with the Array member:

$jsonObj.Array | Foreach-object {
  if( $i -eq 1 ) {
    $_ | Add-Member -MemberType NoteProperty -Name Disk -Value $jsonServerFooObj.Disk
    $i++
  } else {
    $_ | Add-Member -MemberType NoteProperty -Name Disk -Value $jsonServerBooObj.Disk
  }
}

You may also want to consider calling ConvertTo-Json -Depth 100 so you don't get the default object depth which is low, 3 I think.


As noted by @user2864740, in PowerShell 6 ConvertFrom-Json has the -AsHashtable parameter if you would rather work with a Hashtable than a PSCustomObject.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.