JMESPath如何汇总数据

时间:2020-10-05 15:17:09

标签: azure azure-cli jmespath

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

获取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"
  }
]

我希望得到结果= 98 + 10 + 9.7 + 9 + 9 = 135.7

我尝试过的方法:
我调用了sum函数,但是它不起作用

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

并获取消息

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

1 个答案:

答案 0 :(得分:0)

我明白了,因为usageQuantity类型是string,所以必须转换为数字。

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