目标是按百分比对账单费率数组进行排序,同时维护原始 json 对象。
[{
"id": 2,
"employee_observations": [{
"id": 1,
"bill_rates": [{
"bill_classification": "Lawn Mowing",
"percentage": 0.672399672399672
},
{
"bill_classification": "Trimming",
"percentage": 0.00163800163800164
},
{
"bill_classification": "Snow Removal",
"percentage": 0.630630630630631
},
{
"bill_classification": "Rock Removal",
"percentage": 0.00163800163800164
}
]
}]
}]
Run Code Online (Sandbox Code Playgroud)
预期产出
[{
"id": 2,
"employee_observations": [{
"id": 1,
"bill_rates": [
{
"bill_classification": "Lawn Mowing",
"percentage": 0.672399672399672
},
{
"bill_classification": "Snow Removal",
"percentage": 0.630630630630631
},
{
"bill_classification": "Rock Removal",
"percentage": 0.00163800163800164
},
{
"bill_classification": "Trimming",
"percentage": 0.00163800163800164
}
]
}]
}]
Run Code Online (Sandbox Code Playgroud)
如果我将其单独分解,我可以获得预期的结果,但尚未找到正确的组合来保持上面的嵌套版本中格式的完整性。
[
{
"activity_classification": "Lawn Mowing",
"percentage": 0.672399672399672
},
{
"activity_classification": "Trimming",
"percentage": 0.00163800163800164
},
{
"activity_classification": "Snow Removal",
"percentage": 0.630630630630631
},
{
"activity_classification": "Rock Removal",
"percentage": 0.00163800163800164
}
]
Run Code Online (Sandbox Code Playgroud)
jq 'sort_by(.activity_percentage) | jq 'sort_by(.activity_percentage) | 反向' - 结果
[
{
"activity_classification": "Lawn Mowing",
"percentage": 0.672399672399672
},
{
"activity_classification": "Snow Removal",
"percentage": 0.630630630630631
},
{
"activity_classification": "Rock Removal",
"percentage": 0.00163800163800164
},
{
"activity_classification": "Trimming",
"percentage": 0.00163800163800164
}
]
Run Code Online (Sandbox Code Playgroud)
使用更新运算符|=保留整体结构,并使用负比较值-.percentage按降序排序:
jq '.[].employee_observations[].bill_rates |= sort_by(-.percentage)'
Run Code Online (Sandbox Code Playgroud)
[
{
"id": 2,
"employee_observations": [
{
"id": 1,
"bill_rates": [
{
"bill_classification": "Lawn Mowing",
"percentage": 0.672399672399672
},
{
"detection_classification": "Snow Removal",
"percentage": 0.630630630630631
},
{
"detection_classification": "Trimming",
"percentage": 0.00163800163800164
},
{
"detection_classification": "Rock Removal",
"percentage": 0.00163800163800164
}
]
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
995 次 |
| 最近记录: |