如何将jq的任意嵌套值求和?

时间:2018-09-13 21:07:57

标签: json nested sum jq

我一直在努力想出一种用jq对任意嵌套字段求和的方法。两次调用之间“存储桶”的级别有所不同(汇总字段的不同级别)。那么,如何对以下代码段中的所有avg_amount字段求和(理想情况下不对每个.buckets.[]进行硬编码)?

[
   {
      "buckets":[
         {
            "buckets":[
               {
                  "buckets":[
                     {
                        "avg_amount":25,
                     }
                  ],
                  "destination":"Apple"
               },
               {
                  "buckets":[
                     {
                        "avg_amount":74,
                     }
                  ],
                  "destination":"Google"
               }
            ],
            "vendor_name":"Vendor Bee"
         },
         {
            "buckets":[
               {
                  "buckets":[
                     {
                        "avg_amount":1000,
                     }
                  ],
                  "destination":""
               }
            ],
            "vendor_name":""
         }
      ],
      "company_id":1
   }
]

谢谢!

0 个答案:

没有答案