Home > Net >  How to escape specific JSON characters in Powershell
How to escape specific JSON characters in Powershell

Time:05-10

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"}
    ]
}

CodePudding user response:

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"}
  • Related