Nikola Johnson
Nikola Johnson

Reputation: 649

How do I create a JSON in PowerShell?

How do I create a Json in PowerShell?
I assumed it would be similar to creating Xmls in PowerShell but it doesn't work:

[Json]$MyJsonVariable = @"
{
  "MyList"{
    "Item1" {
      "Name": "AMD Ryzen 5 3600x"
      "Type":"CPU"
      "Price":"`$69.99"
      "Where":"Amazon.com"
    }
  }
}
"@

But it didn't work:

Unable to find type [Json].
At line:1 char:1
+ [Json]$MyJsonVariable = @"
+ ~~~~~~
    + CategoryInfo          : InvalidOperation: (Json:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound

Help Please

Upvotes: 14

Views: 56084

Answers (3)

gurpartap
gurpartap

Reputation: 455

Incase you need to use variables, escape backslash, and handle : in url.
Or you get an error: No characters are allowed after a here-string header

@"
{"cluster": "$($m.Matches.Groups[1].Value)", "path": "$([regex]::escape(${client_repo_path}))", "url":"${url}"}
"@ | ConvertFrom-Json

Upvotes: 0

Nico Nekoru
Nico Nekoru

Reputation: 3102

Like the other answer shows, you can use PowerShell hashtables as JSONs, however, PowerShell does have a way to work with JSONs.

First of all, even if there was a JSON datatype in Powershell, you would still be getting a syntax error for your JSON. This is what your JSON should look like:

$MyJsonVariable = @"
{
  "MyList":{
    "Item1":{
      "Name":"AMD Ryzen 5 3600x",
      "Type":"CPU",
      "Price":"`$69.99",
      "Where":"Amazon.com"
    }
  }
}
"@

Notice the commas and the colons before the curly braces.

Once we define this variable, we can convert it to an actual JSON with the ConvertFrom-JSON cmdlet:

$MyJsonVariable = $MyJsonVariable | ConvertFrom-JSON

Which would convert it to a JSON:

PS C:\> $MyJsonVariable.MyList

Item1
-----
@{Name=AMD Ryzen 5 3600x; Type=CPU; Price=$69.99; Where=Amazon.com}


PS C:\> $MyJsonVariable.MyList.Item1

Name              Type Price  Where
----              ---- -----  -----
AMD Ryzen 5 3600x CPU  $69.99 Amazon.com


PS C:\> $MyJsonVariable.MyList.Item1.Where
Amazon.com

And to see the structure of it you can use the ConvertTo-JSON cmdlet:

PS C:\> $testjson | ConvertTo-Json
{
    "MyList":  {
                   "Item1":  {
                                 "Name":  "AMD Ryzen 5 3600x",
                                 "Type":  "CPU",
                                 "Price":  "$69.99",
                                 "Where":  "Amazon.com"
                             }
               }
}

Upvotes: 17

leeharvey1
leeharvey1

Reputation: 1436

Give this a try:

$MyJsonHashTable = @{
  'MyList' = @{
    'Item1' = @{
      'Name' = 'AMD Ryzen 5 3600x'
      'Type' = 'CPU'
      'Price' = '$69.99'
      'Where' = 'Amazon.com'
    }
  }
}

$MyJsonVariable = $MyJsonHashTable | ConvertTo-Json

$err = $null
$MyJsonObject = [Microsoft.PowerShell.Commands.JsonObject]::ConvertFromJson($MyJsonVariable, [ref]$err)

The result is a $MyJsonVariable that contains a Json string:

{
    "MyList":  {
                   "Item1":  {
                                 "Price":  "$69.99",
                                 "Where":  "Amazon.com",
                                 "Name":  "AMD Ryzen 5 3600x",
                                 "Type":  "CPU"
                             }
               }
}

And a $MyJsonObject that contains a Json object reference:

MyList
------
@{Item1=}

I suspect one of these 3 PowerShell variables should be suitable for your Json needs.

Hope this helps.

Upvotes: 22

Related Questions