DEV Community

Wriju's Blog
Wriju's Blog

Posted on

2

Azure CLI Query JSON Output using JMESPath

Azure CLI retuns JSON output bydefault. To find the right information we use JMESPath to query the JSON string. Azure CLI Allows you to pass the value in --query parameter.

Before we jump to the Azure CLI we can dive into the basics of JMESPath to find a bit more details.

Let's assume that we have the below sample JSON

{
    "employees":
    [
        {
            "name": "Wriju", 
            "email": "wriju@contoso.com",
            "city": "Bangalore"        
        },
        {
            "name": "Wrishika", 
            "sal":
            {
                "jan":"100",
                "feb":"200"
            }
        },
        {
            "name": "Wrishika", 
            "email": "wrishika@contoso.com",
            "city": "Mysore",
            "sal":
            {
                "jan":"100",
                "feb":"200"
            }
        },
        {
            "name": "Writam", 
            "email": "writam@contoso.com",
            "city": "Siliguri"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

To get all the values we can write
employees then it will return all the values. If you want to view the values only from field name then you can use

employee[*].name
Enter fullscreen mode Exit fullscreen mode

This would result in as

[
  "Wriju",
  "Wrishika",
  "Wrishika",
  "Writam"
]
Enter fullscreen mode Exit fullscreen mode

Now imagine you want to read the jan value from sal property then you would write

employee[1].sal.jan
Enter fullscreen mode Exit fullscreen mode

so on and so forth.

In Azure CLI we can also use it filter the output result. If I want to get the admin user name of a VM that information is stored in

"osProfile": {
      "adminPassword": null,
      "adminUsername": "wriju",
      "allowExtensionOperations": true,
      "computerName": "wm-Ubuntu18",
      ...
Enter fullscreen mode Exit fullscreen mode

So we can use osProfile.adminUsername

The final Azure CLI would look like,

az vm show -g rg-vm -n wm-Ubuntu18 --query "osProfile.adminUsername"
Enter fullscreen mode Exit fullscreen mode

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay