JSON is very nearly valid PowerShell

by Doug Finke on September 16, 2012

JSON (JavaScript Serialized Object Notation) has become one of the standard formats for sending data by HTTP request between web browsers and other applications. Check out my other post Using PowerShell for dynamic JSON parsing.

An example JSON string

The basic types are objects, lists, strings, numbers, and nulls. All of the keys in an object must be strings.

$json = @"
 "name": "John",
 "places_worked": ["New York", "Boston", "California"],
 "pet": null,
 "family": [{"name": "Eric", "age": 43, "pet": "Rosco"},
              {"name": "Dawn", "age": 47, "pet": "Rocky"}]

PowerShell v3 Supports JSON

To convert a JSON string to a PowerShell object, use ConvertFrom-JSON. This PowerShell cmdlet is delivered out of the box with Version 3.

$json | ConvertFrom-Json            
name places_worked                  pet family                                                            
---- -------------                  --- ------                                                            
John {New York, Boston, California}     {@{name=Eric; age=43; pet=Rosco}, @{name=Dawn; age=47; pet=Rocky}}

Once converted, you can access individual properties of the PowerShell Object.

($json | ConvertFrom-Json).places_worked            
New York

Manually Converting the JSON to PowerShell

Starting with the original JSON example. I’ll remove it from the string and do several edits to make it valid PowerShell.

  1. Adding an @ sign in from of the curly brackets makes it a hashtable
  2. I cast the hashtable to a PowerShell object by using [PSCustomObject]
  3. Replace the ‘:’ with ‘=’. Valid syntax for PowerShell hashtables
  4. We’re building up a hashtable so, remove the ‘,’ or replace the with ‘;’
  5. Change the ‘[]’ JSON array notation with ‘@()’ PowerShell array notation
  6. Add a $ to null
  7. Finally in the family property, I convert JSON Object notation to a PowerShell hashtable and cast it to an object with [PSCustomObject]


$list = [PSCustomObject]@{            
 "name" = "John"            
 "places_worked" = @("New York", "Boston", "California")            
 "pet" = $null            
 "family" = @([PSCustomObject]@{"name" = "Eric"; "age" = 43; "pet"= "Rosco"},            
              [PSCustomObject]@{"name" = "Dawn"; "age" = 47; "pet" = "Rocky"})            

Now we can access individual properties just like before.


name age pet ---- --- --- Eric 43 Rosco Dawn 47 Rocky

Converting PowerShell Objects to JSON

Often coming form PowerShell or .NET we want take our objects and convert them to JSON strings so we can store them or send them over HTTP. PowerShell excels at this on many levels. Lets take our newly created list and use the building ConvertTo-JSON cmdlet to get a JSON string.

$list | ConvertTo-Json            
    "name":  "John",
    "places_worked":  [
                          "New York",
    "pet":  null,
    "family":  [
                       "name":  "Eric",
                       "age":  43,
                       "pet":  "Rosco"
                       "name":  "Dawn",
                       "age":  47,
                       "pet":  "Rocky"

This string is ready to be sent over HTTP to an any endpoint that can handle JSON. See my other post Using PowerShell for dynamic JSON parsing.

Want more and Get 50% off?

My new book “PowerShell for Developers” covers these, other PowerShell v3 features and more. Use code DSUG for the discount.

  • Slice and dice Text, XML, CSV, and JSON effortlessly
  • Embed PowerShell to provide scripting abilities for your C# apps
  • Create GUI applications five to ten times faster with less code
  • Leverage PowerShell’s capabilities to work with the Internet
  • Interact with DLLs and create objects, automatically display properties, and call methods in live interactive sessions
  • Build domain-specific languages (DSLs) and vocabularies to express solutions more clearly
  • Work with Microsoft Office via the Component Object Model (COM)
  • Discover PowerShell v3 features included with Windows 8 and Windows Server 2012

{ 2 trackbacks }

The Morning Brew - Chris Alcock » The Morning Brew #1193
09.20.12 at 3:34 am
Blog J.Schweiss
11.09.13 at 9:10 am

{ 0 comments… add one now }

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>