How JMESPath summary data

189 views Asked by At
az consumption usage list -m --query "sort_by([],&usageQuantity)[-5:].{name:instanceName,ser:consumedService,cat:meterDetails.meterCategory,qty:usageQuantity}"

get json :

[
  {
    "cat": "Azure App Service",
    "name": "weiwebapp02serviceplan",
    "qty": "9",
    "ser": "microsoft.web"
  },
  {
    "cat": "Azure App Service",
    "name": "weiwebapp01serviceplan",
    "qty": "9",
    "ser": "microsoft.web"
  },
  {
    "cat": "Virtual Machines",
    "name": "demoVM",
    "qty": "9.7",
    "ser": "Microsoft.Compute"
  },
  {
    "cat": "Bandwidth",
    "name": "weiwebapp01",
    "qty": "10",
    "ser": "microsoft.web"
  },
  {
    "cat": "Container Registry",
    "name": "42556f4129fb4c5db21e365ea2770211",
    "qty": "98",
    "ser": "Microsoft.ContainerRegistry"
  }
]

I hope get result = 98 + 10 + 9.7 + 9 + 9 = 135.7

What I've tried :
I called sum function but it not work

az consumption usage list -m --query "sort_by([],&usageQuantity)[-5:].qty:usageQuantity | sum()"

and get message

UnexpectedError: The command failed with an unexpected error. Here is the traceback:
Expected 1 argument for function sum(), received 0
1

There are 1 answers

0
Wei Lin On BEST ANSWER

I got the point, because usageQuantity type is string , so it have to cast to number.

az consumption usage list -m --query "sort_by([],&usageQuantity)[-5:].usageQuantity | [].to_number(@) | sum([]) ""