Playing with JSON and PowerShell


Summary: Ed Wilson, Microsoft Scripting Guy, talks about playing with JSON and Windows PowerShell 5.0.

Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife has an updated shopping list. It includes a Microsoft Band 2 and a Surface Pro 4. The launch was a mouthwatering event and really well done.

One of the cool cmdlets in Windows PowerShell 5.0 on Windows 10 is the ConvertFrom-JSON cmdlet. One reason it is cool is that it will convert a Java Script Object Notation (JSON) string into a custom Windows PowerShell object. This is a cool way to interact with web services, and it can save a bit of time from parsing XML. In fact, it is remarkably easy to do.

The band, man, the band!

As an example, I am going to use a beta JSON interface to the MusicBrainz web service. This permits me to use Invoke-WebRequest and return a JSON formatted string.

The following request performs an artist lookup and returns a description, name, and country of origin (in addition to other stuff) about the artist. The query contains the website, the API, the artist information, and the format requested. Here is the query string:

$request = 'http://musicbrainz.org/ws/2/artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da?inc=aliases&fmt=json'

To execute this query is simple, I use Invoke-WebRequest. This appears here:

Invoke-WebRequest $request

Because I requested the data to return as JSON, I need to convert it from JSON. I use the new ConvertFrom-JSON cmdlet and select the three fields I am interested in looking at:

ConvertFrom-Json |

Select name, disambiguation, country

The script, and the output from the script are shown here:

Image of command output

If I did not convert from JSON, the output would look like this:

Image of command output

The returned and the converted from JSON output are shown here:

Image of command output

I can also find specific information about recordings by querying the Recording interface. The procedure is exactly the same. I store my query string, use Invoke-WebRequest, convert the output to JSON, and select the properties I am interested in.

Because the properties about the recording I am interested in are stored in the Releases property as embedded objects, I need to expand that property, and then choose what I want from the embedded object. Here is that code:

$request = 'http://musicbrainz.org/ws/2/recording/fcbcdc39-8851-4efc-a02a-ab0e13be224f?inc=artist-credits+isrcs+releases&fmt=json'

Invoke-WebRequest $request |

ConvertFrom-Json  |

select -expand releases |

Select title, date, country

The script and the output are shown here:

Image of command output

There are other sample queries on the MusicBrainz.Org JSON site that you can play around with to get familiar with parsing JSON. It is fun, interesting, and pretty easy to do—great combination, if you ask me.

That is all there is to using a web request and returning JSON. Join me tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Comments (4)

  1. Yoshihiro Kawabata says:

    Can I update the "Microsoft.PowerShell.Utility" module from 3.1.0.0 to 5.* to use ConvertFrom-Json method ?
    I want to use ConvertFrom-Json methods, etc.
    My PC = Windows 10 Home x64.

  2. Laurence Benjamin says:

    You are in luck, this cmdlet is actually available from PowerShell 3 and up. The PowerShell 5 version however has a slight improvement (https://technet.microsoft.com/en-gb/library/hh857339.aspx)
    "ConvertTo-Json and ConvertFrom-Json can now accept terms within double quotes, and its error messages are now localizable"

    For this specific use case, ie. JSON data from web server, you can also use the "Invoke-RestMethod" cmdlet, which is also available from PowerShell 3 and up (https://technet.microsoft.com/en-us/library/hh849971.aspx)
    which handles, simultaneously, both the download and conversion of data in JSON (XML, RSS and ATOM) format.

  3. Yoshihiro Kawabata says:

    Thank you Laurence Benjamin, for your quick response.
    I start to use ConvertFrom-Json to consume some Web Api service.

  4. MichaelRubin says:

    Nice article, http://jsonformatter.org will help to format json data

Skip to main content