Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

Playing with JSON and PowerShell

$
0
0

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


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>