1. Home
  2. Training Library
  3. Microsoft Azure
  4. Courses
  5. Getting Started with Azure Cross-Platform CLI Automation

Demo: Leveraging JSON Output in Azure xPlat CLI

Start course
Overview
DifficultyAdvanced
Duration59m
Students291

Description

In this demonstration, we will take a look at the JSON output support in the Azure Cross-Platform (xPlat) CLI Tool. By emitting JSON from the Azure xPlat CLI Tool, we can use other JSON parsing tools, such as jsawk or jq, to filter, modify, and sort the data, reducing the need to perform onerous text parsing, and improving the output format of the data.

Transcript

One of the great things about the Azure cross-platform CLI tool is that we can emit JSON from many of the commands that are available inside of it.

So the first thing we're going to do is call "Azure config help," and we're actually going to change the mode from Azure service management, which we're currently in, over to the Azure resource manager mode. So now we've changed contexts into ARM successfully. Any commands that we issue from now on will be executed against the resource manager API instead of the classic service management API.

So if we call "Azure account list," you'll see that we get more human readable output in a tabular format from that command. Now, what's cool is that if you want to pass data between different commands in a bash shell, you can actually emit JSON instead. So if we call "Azure account list --JSON," you'll see that we get the JavaScript object notation for those objects that are emitted.

Now, this command, or this parameter, rather, works on many other Azure commands, as well. So one of the key concepts in Azure resource manager is called a resource group, which contains individual resources. So if we call "Azure group list," we'll get a list of resource groups inside of our subscription. If we append the "--JSON" parameter onto that, you'll actually see that we get JSON output for each of those resource groups. So the name, location, and provisioning state, as well as the tags, will be emitted as JSON, which is much easier to parse using other command line tools like JQ or the JS Awk utility that parse JSON output instead of tabular output.

As a practical example of how to use the JSON output, let's go ahead and use the utility called JS Awk. As you can see, I've got JS Awk installed on my system, and so I can use that to parse these JSON objects. So I'm going to call "Azure group list --JSON," and you'll see the output of the command. So each of these JSON objects is a member of an outer array. So the array can be parsed using a tool like JS Awk. So if we pipe the JSON output into JS Awk, we can run JavaScript for each object that's in the JSON array.

So let's go ahead and just do something really simple, like "return this.name." So the JS Awk utility is going to look at the array of JSON objects, and for each object, it's simply going to return the name property. So let's see what the output of that looks like. So as you can see, the JS Awk utility has returned just the name property from each object, each resource group, in this case, and it's actually built a JSON array containing strings that represent the names of each resource group.

You can also modify objects in the pipeline using JSON, the JS Awk utility, so we can actually add another property to each of the objects. So, for example, we could call "this.company," and set that equal to Cloud Academy. So what JS Awk is going to do is take each of the objects in this JSON pipeline, and it's going to create a new property called "this.company." So if we run that command, we'll get very similar JSON output to what we had before, but if we look through the rather complicated output here, you'll see that we have a property called "company," and each of these resource groups in this array of objects has a new property called "company" now.

One other thing that we can do with JS Awk is to run an after script. So basically, after each object has been processed in the pipeline...so let's go back to our earlier example of returning just the resource group names. Once we get this array of strings back, we can run what's called an after script, and we can "return this.join," and then we'll basically join all these strings on a new line character.

So now what we've done is we've taken the JSON output for these resource groups, we've parsed it with the JS Awk utility to just return the name property, and then finally, we took that array of strings and we joined it on a new line character. So now we have just the resource group names, and we can pipe these into other Unix commands to manage them appropriately.

About the Author

Trevor Sullivan is a Microsoft MVP for Windows PowerShell, and enjoys working with cloud and automation technologies. As a strong, vocal veteran of the Microsoft-centric IT field since 2004, Trevor has developed open source projects, provided significant amounts of product feedback, authored a large variety of training resources, and presented at IT functions including worldwide user groups and conferences.