Working with JSON in PowerShell

In last post, we have described on what JSON is and how it is becoming increasingly important in the DevOps phenomenon. So its necessarily not only to understand the format, but also involve in our tools of choices. Microsoft has introduced couple of cmdlets in PowerShell to make use of JSON format namely ConvertFrom-JSON and ConvertTo-JSON.

Again, we can get the same by making use of Get-Command cmdlet:

JSON cmdlets in PowerShell
JSON cmdlets in PowerShell

As an example, we are going to start with below sample data object:

{
"ts":1409318650332,
"userId":"309",
"sessionId":1879,
"page":"NextSong",
"auth":"Logged In",
"method":"PUT",
"status":200,
"level":"free",
"itemInSession":2,
"location":"Killeen-Temple, TX",
"lastName":"Montgomery",
"firstName":"Annalyse",
"registration":1384448062332,
"gender":"F",
"artist":"El Arrebato",
"song":"Quiero Quererte Querer",
"length":234.57914
}

So, first we need to store this inside a variable:

$data = '{"ts":1409318650332,"userId":"309","sessionId":1879,"page":"NextSong","auth":
"Logged In","method":"PUT","status":200,"level":"free","itemInSession":2,"location":"Killeen-Temple, TX","lastName":"Mon
tgomery","firstName":"Annalyse","registration":1384448062332,"gender":"F","artist":"El Arrebato","song":"Quiero Quererte
 Querer","length":234.57914}'

Storing JSON data inside a variable

Note that we have to use single quotation around data so that it does not confuse our PowerShell. Now, we need to import this data inside a another variable, using ConvertFrom-JSON cmdlet:

$psdata = ConvertFrom-JSON -InputObject $data

Once its done, we can see the datatype of variable using gettype() method:

Checking datatype of variable
Checking datatype of variable

We can also see the properties using Get-Member cmdlet and verify that data is imported correctly:

$psdata | gm

After this, we can simple leverage dot notation to access objects:

Verifying data imported and accessing the data

Let’s assume some real world data using PowerShell. For this, we will make use of MusicBrainz web services API. (More details at: https://wiki.musicbrainz.org/Development/JSON_Web_Service)

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, we use Invoke-WebRequest. This appears here:

Invoke-WebRequest $request
Requesting data from MusicBrainz using REST APIs in PowerShell
Requesting data from MusicBrainz using REST APIs in PowerShell

If we just see the output as of now, it would look like:

Output data from WebRequest cmdlet
Output data from WebRequest cmdlet

We now need to convert from JSON format. For this, we can use:

$sitedata = ConvertFrom-JSON $data
Requesting data from MusicBrainz using REST APIs in PowerShell - 2.PNG
Requesting data from MusicBrainz using REST APIs in PowerShell – 2.PNG

We can now use simple formatting in PowerShell to read this data further.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s