Subscribe via Email

Subscribe via RSS/JSON


Creative Commons Attribution 4.0 International License
© Rakhesh Sasidharan


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!

Search Firefox bookmarks using PowerShell

I was on Firefox today and wanted to search for a bookmark. I found the bookmark easily, but unlike Chrome Firefox has no way of showing which folder the bookmark is present in. So I created a PowerShell script to do just that. Mainly because I wanted some excuse to code in PowerShell and also because it felt like an interesting problem.

PowerShell can’t directly control Firefox as it doesn’t have a COM interface (unlike IE). So you’ll have to manually export the bookmarks. Good for us the export is into a JSON file, and PowerShell can read JSON files natively (since version 3.0 I think). The ConvertFrom-JSON cmdlet is your friend here. So export bookmarks and read them into a variable:

This gives me a tree structure of bookmarks.

Notice the children property. It is an array of further objects – links to the first-level folders, basically. Each of these in turn have links to the second-level folders and so on.

The type property is a good way of identifying if a node is a folder or a bookmark. If it’s text/x-moz-place-container then it’s a folder. If it’s text/x-moz-place then it’s a bookmark. (Alternatively one could also test whether the children property is $null).

So how do we go through this tree and search each node? Initially I was going to iteratively do it by going to each node. Then I remembered recursion (see! no exercise like this goes wasted! I had forgotten about recursion). So that’s easy then.

  • Make a function. Pass it the bookmarks object.
  • Said function looks at the object passed to it.
    • If it’s a bookmark it searches the title and lets us know if it’s a match.
    • If it’s a folder, the function calls itself with the next level folder as input.

Here’s the function:

I decided to search folder names too. And just to distinguish between folder names and bookmark names, I use different colors.

Call the function thus (after exporting & reading the bookmarks into a variable):

Here’s a screenshot of the output: