Json:
[
{
"account": "1",
"cost": [
{
"usage":"low",
"totalcost": "2.01"
}
]
},
{
"account": "2",
"cost": [
{
"usage":"low",
"totalcost": "2.25"
}
]
},
{
"account": "1",
"cost": [
{
"usage":"low",
"totalcost": "15"
}
]
},
{
"anotheraccount": "a",
"cost": [
{
"usage":"low",
"totalcost": "2"
}
]
}
]
预期结果:
account cost
1 17.01
2 2.25
anotheraccount cost
a 2
我能够提取数据,但不确定如何汇总。
jq '.[] | {account,cost : .cost[].totalcost}'
使用jq可以做到这一点吗,所以我可以获得与它们相关的所有类型的帐户和成本?
答案 0 :(得分:1)
两个帮助器功能将帮助您到达目的地:
@NgModule({
declarations: [
MyComponent
],
imports: [
BrowserModule,
FormsModule,
HttpClientModule
],
providers: []
})
export class AppModule { }
使用这些,以下调用:
def sigma( f ): reduce .[] as $o (null; . + ($o | f )) ;
def group( keyname ):
map(select(has(keyname)))
| group_by( .[keyname] )
| map({(keyname) : .[0][keyname],
cost: sigma(.cost[].totalcost | tonumber) })
;
产量:
group("account"),
group("anotheraccount")
您应该能够在jq中管理最后的格式化步骤。