jq:使用数学函数减少json数组
我编写了这个JSONProcessor bash函数来根据名称值对数组进行求和、求平均、获取最小值和获取最大值。平均值和最小值都不正确,我一辈子都搞不清楚自己做错了什么jq:使用数学函数减少json数组,json,jq,Json,Jq,我编写了这个JSONProcessor bash函数来根据名称值对数组进行求和、求平均、获取最小值和获取最大值。平均值和最小值都不正确,我一辈子都搞不清楚自己做错了什么 function JSONProccessor { jq ' def myMathFunc: if (.name | test("^sum", "")) then {"\(.name)": (.values | add)} elif (.na
function JSONProccessor {
jq '
def myMathFunc:
if (.name | test("^sum", "")) then
{"\(.name)": (.values | add)}
elif (.name | test("^avg|^global-avg", "")) then
{"\(.name)": ((.values | add) / (.values | length)) }
elif (.name | test("^max", "")) then
{"\(.name)": (.values | max) }
elif (.name | test("^min", "")) then
{"\(.name)": (.values | min) }
else
{"\(.name)": .values[]}
end;
[
.Response.stats.data[] |
.identifier.names[] as $name |
.identifier.values[] as $val |
{"\($name)": "\($val)"} + ([
.metric[] | myMathFunc
] | add)
]
' < ${1} > ${2}
}
输出
[
{
"apiproxy": "authn",
"min(request_processing_latency)": 923,
"avg(total_response_time)": 2216.5,
"max(request_processing_latency)": 1292,
"global-avg-total_response_time": 1473.3333333333333,
"sum(message_count)": 3
}
]
如果我遗漏了任何明显的东西,请你检查一下并告诉我好吗 您的想法是正确的,但是test函数不需要您定义的第二个参数。您只需要使用test函数返回bool来断言匹配。删除第二个参数会使函数按预期工作 测试功能支持原型testREGEX;中定义的标志与您的逻辑无关。测试函数甚至不接受由分隔的参数,而只接受由分隔的参数;解除限制器
[
{
"apiproxy": "authn",
"min(request_processing_latency)": 923,
"avg(total_response_time)": 2216.5,
"max(request_processing_latency)": 1292,
"global-avg-total_response_time": 1473.3333333333333,
"sum(message_count)": 3
}
]