如何使用 jq 对 json 中的值求和
How to sum values in json using jq
我刚开始使用 jq 解析器,因为我需要从 json 文件中解析一些值并进行少量更改并输出包含更改的更新后的 json 文件。这是我正在处理的许多样本中的一个。
输入json:
{
"name": ".",
"metadata": {
"path": ".",
"type": "terraform_dir",
"terraformWorkspace": "default"
},
"pastBreakdown": {
"resources": [],
"totalHourlyCost": "0",
"totalMonthlyCost": "0"
},
"breakdown": {
"resources": [
{
"name": "vm01",
"metadata": {},
"hourlyCost": "0.007989726027400584",
"monthlyCost": 3.9660999999999986,
"costComponents": [
{
"name": "vm01-201",
"unit": "years",
"hourlyQuantity": "0.0001141552511416",
"monthlyQuantity": "0.0833333333333333",
"price": "69.99",
"hourlyCost": "0.007989726027400584",
"monthlyCost": "5.832499999999997667"
}
]
},
{
"name": "public_ip.example",
"metadata": {},
"hourlyCost": "0.005",
"monthlyCost": 2.482,
"costComponents": [
{
"name": "IP address (static)",
"unit": "hours",
"hourlyQuantity": "1",
"monthlyQuantity": "730",
"price": "0.005",
"hourlyCost": "0.005",
"monthlyCost": "3.65"
}
]
},
{
"name": "storage_account",
"metadata": {},
"hourlyCost": "0.17575342465753424425",
"monthlyCost": 87.24400000000001,
"costComponents": [
{
"name": "Write",
"unit": "10K operations",
"hourlyQuantity": "0.136986301369863",
"monthlyQuantity": "100",
"price": "0.1",
"hourlyCost": "0.0136986301369863",
"monthlyCost": "10"
},
{
"name": "List",
"unit": "10K operations",
"hourlyQuantity": "0.136986301369863",
"monthlyQuantity": "100",
"price": "0.05",
"hourlyCost": "0.00684931506849315",
"monthlyCost": "5"
},
{
"name": "Read",
"unit": "10K operations",
"hourlyQuantity": "0.0136986301369863",
"monthlyQuantity": "10",
"price": "0.01",
"hourlyCost": "0.000136986301369863",
"monthlyCost": "0.1"
},
{
"name": "All other operations",
"unit": "10K operations",
"hourlyQuantity": "0.136986301369863",
"monthlyQuantity": "100",
"price": "0.004",
"hourlyCost": "0.000547945205479452",
"monthlyCost": "0.4"
},
{
"name": "retrieval",
"unit": "GB",
"hourlyQuantity": "1.3698630136986301",
"monthlyQuantity": "1000",
"price": "0.01",
"hourlyCost": "0.013698630136986301",
"monthlyCost": "10"
},
{
"name": "write",
"unit": "GB",
"hourlyQuantity": "1.3698630136986301",
"monthlyQuantity": "1000",
"price": "0.0025",
"hourlyCost": "0.00342465753424657525",
"monthlyCost": "2.5"
},
{
"name": "index",
"unit": "10K tags",
"hourlyQuantity": "0.0136986301369863",
"monthlyQuantity": "10",
"price": "0.03",
"hourlyCost": "0.000410958904109589",
"monthlyCost": "0.3"
}
]
}
],
"totalMonthlyCost": "sum value"
从上面json我需要总结每个资源的monthlyCost
并更新totalMonthlyCost
中的总价值
因此输入将是 "3.9660999999999986 + 2.482 + 87.24400000000001"
这些值的总和应在 totalMonthlyCost
中更新
我一直在尝试各种选择,但没有成功。我可以计算数组中的值
但不确定如何更新 totalMonthlyCost
中的值
按照任务描述分为两步:
(.breakdown.resources | map(.monthlyCost) | add) as $sum
| .breakdown.totalMonthlyCost = $sum
这样可以不用中间变量写成:
.breakdown.totalMonthlyCost
= (.breakdown.resources | map(.monthlyCost) | add)
或更多 DRYly:
.breakdown
|= (.totalMonthlyCost = (.resources | map(.monthlyCost) | add))
我刚开始使用 jq 解析器,因为我需要从 json 文件中解析一些值并进行少量更改并输出包含更改的更新后的 json 文件。这是我正在处理的许多样本中的一个。
输入json:
{
"name": ".",
"metadata": {
"path": ".",
"type": "terraform_dir",
"terraformWorkspace": "default"
},
"pastBreakdown": {
"resources": [],
"totalHourlyCost": "0",
"totalMonthlyCost": "0"
},
"breakdown": {
"resources": [
{
"name": "vm01",
"metadata": {},
"hourlyCost": "0.007989726027400584",
"monthlyCost": 3.9660999999999986,
"costComponents": [
{
"name": "vm01-201",
"unit": "years",
"hourlyQuantity": "0.0001141552511416",
"monthlyQuantity": "0.0833333333333333",
"price": "69.99",
"hourlyCost": "0.007989726027400584",
"monthlyCost": "5.832499999999997667"
}
]
},
{
"name": "public_ip.example",
"metadata": {},
"hourlyCost": "0.005",
"monthlyCost": 2.482,
"costComponents": [
{
"name": "IP address (static)",
"unit": "hours",
"hourlyQuantity": "1",
"monthlyQuantity": "730",
"price": "0.005",
"hourlyCost": "0.005",
"monthlyCost": "3.65"
}
]
},
{
"name": "storage_account",
"metadata": {},
"hourlyCost": "0.17575342465753424425",
"monthlyCost": 87.24400000000001,
"costComponents": [
{
"name": "Write",
"unit": "10K operations",
"hourlyQuantity": "0.136986301369863",
"monthlyQuantity": "100",
"price": "0.1",
"hourlyCost": "0.0136986301369863",
"monthlyCost": "10"
},
{
"name": "List",
"unit": "10K operations",
"hourlyQuantity": "0.136986301369863",
"monthlyQuantity": "100",
"price": "0.05",
"hourlyCost": "0.00684931506849315",
"monthlyCost": "5"
},
{
"name": "Read",
"unit": "10K operations",
"hourlyQuantity": "0.0136986301369863",
"monthlyQuantity": "10",
"price": "0.01",
"hourlyCost": "0.000136986301369863",
"monthlyCost": "0.1"
},
{
"name": "All other operations",
"unit": "10K operations",
"hourlyQuantity": "0.136986301369863",
"monthlyQuantity": "100",
"price": "0.004",
"hourlyCost": "0.000547945205479452",
"monthlyCost": "0.4"
},
{
"name": "retrieval",
"unit": "GB",
"hourlyQuantity": "1.3698630136986301",
"monthlyQuantity": "1000",
"price": "0.01",
"hourlyCost": "0.013698630136986301",
"monthlyCost": "10"
},
{
"name": "write",
"unit": "GB",
"hourlyQuantity": "1.3698630136986301",
"monthlyQuantity": "1000",
"price": "0.0025",
"hourlyCost": "0.00342465753424657525",
"monthlyCost": "2.5"
},
{
"name": "index",
"unit": "10K tags",
"hourlyQuantity": "0.0136986301369863",
"monthlyQuantity": "10",
"price": "0.03",
"hourlyCost": "0.000410958904109589",
"monthlyCost": "0.3"
}
]
}
],
"totalMonthlyCost": "sum value"
从上面json我需要总结每个资源的monthlyCost
并更新totalMonthlyCost
因此输入将是 "3.9660999999999986 + 2.482 + 87.24400000000001"
这些值的总和应在 totalMonthlyCost
我一直在尝试各种选择,但没有成功。我可以计算数组中的值
但不确定如何更新 totalMonthlyCost
按照任务描述分为两步:
(.breakdown.resources | map(.monthlyCost) | add) as $sum
| .breakdown.totalMonthlyCost = $sum
这样可以不用中间变量写成:
.breakdown.totalMonthlyCost
= (.breakdown.resources | map(.monthlyCost) | add)
或更多 DRYly:
.breakdown
|= (.totalMonthlyCost = (.resources | map(.monthlyCost) | add))