0

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.

1
  • 1
    Are you sure you want string representation of your objects like @{Name=Test....} ? You will need to parse them after, I would recommend you to export them as real Json representations of the object Commented Oct 6, 2022 at 3:34

1 Answer 1

1

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.

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

1 Comment

Thank you so much, both, for this help! Works as I needed!

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.