VMware Cloud Community
virtualhobbit
Enthusiast
Enthusiast
Jump to solution

Unable to output JSON correctly

Hi,

I'm trying to defne a JSON object directly in a script and then output it:

$jsonData = @"

{

    "Base":  {

                 "accessGroup":  "Root",

                 "description":  null,

                 "name":  "VDI001",

                 "displayName":  "POC"

             }

}

"@

$jsonData | ConvertTo-Json | Set-Content VDI001.json

However, what I get back is a messed up file with line breaks and the like:

"{\r\n\"Base\":  {\r\n             \"accessGroup\":  \"Root\",\r\n             \"description\":  null,\r\n             \"name\":  \"VDI001\",\r\n             \"displayName\":  \"POC\"\r\n         }\r\n}"

What am I doing wrong?

-Mark

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You are converting a JSON formatted string to JSON.

This should be sufficient to create a JSON file in your example.

$jsonData | Set-Content VDI001.json

The other option is to create a PS object and convert that to JSON

$psData = @{

    Base = @{

        accessGroup = 'Root'

        description = $null

        name = 'VDI001'

        displayName = 'POC'

    }

}

$psData | ConvertTo-Json


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
1 Reply
LucD
Leadership
Leadership
Jump to solution

You are converting a JSON formatted string to JSON.

This should be sufficient to create a JSON file in your example.

$jsonData | Set-Content VDI001.json

The other option is to create a PS object and convert that to JSON

$psData = @{

    Base = @{

        accessGroup = 'Root'

        description = $null

        name = 'VDI001'

        displayName = 'POC'

    }

}

$psData | ConvertTo-Json


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos