0

Background

I am using PowerShell 7.

Yesterday, I asked this question on help merging some JSON together and saving it, it worked great.

I have now ran into another problem that my new gained knowledge doesn't help with - escaped characters.

Problem

I have some JSON like so:

{
    "toolcache": [
        {
            "name": "Python",
            "url": "https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json",
            "platform": "linux",
            "platform_version": "18.04",
            "arch": "x64",
            "versions": [
                "2.7.*",
                "3.6.*",
                "3.7.*",
                "3.8.*",
                "3.9.*",
                "3.10.*"
            ]
        }
    ],
     "powershellModules": [
        {"name": "MarkdownPS"},
        {"name": "Microsoft.Graph"},
        {"name": "Pester"},
        {"name": "PSScriptAnalyzer"}
    ]
}

And I try to apply the knowledge in the other post:


### Add to JSON Toolet

# Try to escape characters as plain text using heredoc - Is this is the issue?
$ToolsetsToAdd = @"
{"name": "SqlServer"}
"@


$ToolsetFile = "toolset-2004.json"
$ToolsetObject = $(Get-Content $ToolsetFile -Raw)
$ToolSetSystemObject =$($ToolsetObject | ConvertFrom-Json)

$ToolSetSystemObject.powershellModules += $ToolsetsToAddJson

$ToolSetSystemObject | ConvertTo-Json -Depth 100 | Set-Content $ToolsetFile

And when I run it, I get:

{
    "toolcache": [
        {
            "name": "Python",
            "url": "https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json",
            "platform": "linux",
            "platform_version": "18.04",
            "arch": "x64",
            "versions": [
                "2.7.*",
                "3.6.*",
                "3.7.*",
                "3.8.*",
                "3.9.*",
                "3.10.*"
            ]
        }
    ],
    "powershellModules": [
    {
      "name": "MarkdownPS"
    },
    {
      "name": "Microsoft.Graph"
    },
    {
      "name": "Pester"
    },
    {
      "name": "PSScriptAnalyzer"
    },
    "{\"name\": \"SqlServer\"}"
  ],

I have tried a variation of escapes using things like `"`" but couldn't figure this one out either.

This to my knowledge is expanded JSON, so I'm not sure its an error, but its not copying and escaping things like I expected. Whether this is avoidable is what I want to find out from someone who knows better!

What I want to do:

Ideally, I'd like a correctly formatted and parsed JSON file like so:

{
    "toolcache": [
        {
            "name": "Python",
            "url": "https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json",
            "platform": "linux",
            "platform_version": "18.04",
            "arch": "x64",
            "versions": [
                "2.7.*",
                "3.6.*",
                "3.7.*",
                "3.8.*",
                "3.9.*",
                "3.10.*"
            ]
        }
    ],
     "powershellModules": [
        {"name": "MarkdownPS"},
        {"name": "Microsoft.Graph"},
        {"name": "Pester"},
        {"name": "PSScriptAnalyzer"}
        {"name": "SqlServer"}
    ]
}
1
  • You want $ToolsetsToAdd = @{name = "SqlServer"} - you're adding it to the existing data after converting from string to object, so you want to add an object (not a string) to it. Commented May 10, 2022 at 9:31

1 Answer 1

2

You are treating the array in $ToolSetSystemObject.powershellModules as if it were an array of strings, but in fact this is an array of objects

To add a new object to it, just do

$ToolSetSystemObject.powershellModules += @{name = "SqlServer"}

or

$ToolSetSystemObject.powershellModules += [PsCustomObject]@{name = "SqlServer"}
Sign up to request clarification or add additional context in comments.

1 Comment

Aaaaand it worked. Thank you!, Yeah, it is a running theme of me mixing up PowerShell Objects with JSON ones, I understanding now that there must be a conversion between the 2 and that is why my script keeps failing. This is much clearer now. The format of this expanded JSON really got me. Thank you!

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.