Contact

Subscribe via Email

Subscribe via RSS/JSON

Categories

Creative Commons Attribution 4.0 International License
© Rakhesh Sasidharan

Elsewhere

Az CLI find location shortcode

I wanted to use the Az CLI command to find the shortcode of a location. You know: “UAE North” == “uaenorth”

This command gives you a list of locations and their shortcode and other details:

This is fine and I could scroll through the output to see the shortcode I was interested in, but I thought it would be fun if I can pipe it through jq to get exactly what I want.

That didn’t work though as I kept getting the following:

Turns out this is because jq cannot parse the input correctly. This stumped me for a while until I realized that the Az commands have an “–output json” switch to output in JSON. So while the default output looks like it’s JSON, it’s actually formatted and not really JSON.

Here’s a typical output entry for a location:

What I get is an array of JSON entries like the above.

If I want a list of locations and their shortcodes I can do something like the following:

What I am doing here is piping this to jq. And I tell jq to expect an array and iterate through it (that’s what the .[] does). Then I tell jq to select just the “displayName” and “name” elements from each JSON entry in this array, call these “displayName” and “name”, and output this as JSON (that’s what the {} around part of the commands do). The output looks like this:

If I don’t want an output as JSON I can skip the {} and do something like this:

This just returns a list of locations and shortcodes and isn’t very pretty:

Now, back to my original problem. I am only interested in the shortcode of the location I am interested in, so I don’t really need a list. Instead I can do something like this:

I use the select operator here to find the entry whose “displayName” matches what I am interested in. The result is a single shortcode.

If I want the result to be JSON I can enclose it around {} like I did earlier and also add a label. 

The result looks like this:

To make it easier for the future, I created a Bash function out of this:

It’s similar to the above just that I have to pass the argument I give the function over to jq. For this I have to use the --args switch which lets me define a new internal variable called “displayName” which has the value of the argument I give the function ($1 in this case, in quotes so I capture strings with spaces). When I use this variable as $displayName in jq later, I should skip the double quotes (figured this out via trial and error).

That’s all, now I can simply do az_location "UAE North" and get my answer!