Home > Software design >  Powershell - Add another object to a JSON with multiple sections?
Powershell - Add another object to a JSON with multiple sections?

Time:10-06

I am extracting a JSON from a file using the following syntax:

$json = Get-Content $jsonfile | Out-String | ConvertFrom-Json

This is what my JSON currently looks like:

{
    "Server":  {
                   "CustomModules":  [
                                         "@{Name=Test Server Module 1; Tag=Server; Action=Option I want; Image_Path=C:Temp; Admin_Only=false}",
                                         "@{Name=Test Server Module 2; Tag=Server; Action=Option I want; Image_Path=C:Temp/Downloads; Admin_Only=false}"
                                     ]
               },
    "Help Desk":  {
                      "CustomModules":  [
                                            "@{Name=Test Help Desk Module 1; Tag=HD; Action=Option I want; Image_Path=C:Temp; Admin_Only=false}",
                                            "@{Name=Test Help Desk Module 2; Tag=HD; Action=Option I want; Image_Path=C:Temp/Downloads; Admin_Only=true}"
                                        ]
                  }
}

How would I add another set of values under the Server 'CustomModules' so it looks like this?

    "Server":  {
                   "CustomModules":  [
                                         "@{Name=Test Server Module 1; Tag=Server; Action=Option I want; Image_Path=C:Temp; Admin_Only=false}",
                                         "@{Name=Test Server Module 2; Tag=Server; Action=Option I want; Image_Path=C:Temp/Downloads; Admin_Only=false}",
                                         "@{Name=Test Server Module 3; Tag=Server; Action=Option I want; Image_Path=C:/User/TestUser; Admin_Only=true}"
                                     ]
               },
    "Help Desk":  {
                      "CustomModules":  [
                                            "@{Name=Test Help Desk Module 1; Tag=HD; Action=Option I want; Image_Path=C:Temp; Admin_Only=false}",
                                            "@{Name=Test Help Desk Module 2; Tag=HD; Action=Option I want; Image_Path=C:Temp/Downloads; Admin_Only=true}"
                                        ]
                  }
}

I've tried Add-Member but it only adds to the current values already there.

CodePudding user response:

You can append objects into the array CustomModules array using this as an example:

$json = Get-Content path\to\json.json -Raw | ConvertFrom-Json
$json.Server.CustomModules = @(
    $json.Server.CustomModules
    ([pscustomobject]@{
        Name       = 'Test Server Module 3'
        Tag        = 'Server'
        Action     = 'Option I want'
        Image_Path = 'C:/User/TestUser'
        Admin_Only = $true
    })
)
$json | ConvertTo-Json

However, as recommended in comments, your arrays currently have string representations of PowerShell objects, most likely due to serialization exceeded the default depth of 2. So to avoid this, because you will need to parse them later, use ConvertTo-Json -Depth 99 when exporting the initial Json.

  • Related