【发布时间】:2018-06-14 19:59:57
【问题描述】:
拥有一个 JSON 对象列表,并尝试根据最小值检查和两个字段之间的比较来过滤它们。
{
"preview": false,
"init_offset": 0,
"messages": [],
"fields": [
{
"name": "A"
},
{
"name": "B"
},
{
"name": "Diff"
},
{
"name": "Threshold"
}
],
"results": [
{
"A": "foo",
"B": "bar",
"Diff": "1095",
"Threshold": "1200"
},
{
"A": "baz",
"B": "bux",
"Diff": "81793",
"Threshold": "0"
},
{
"A": "quux",
"B": "quuz",
"Diff": "194"
},
{
"A": "moo",
"B": "goo",
"Diff": "5000",
"Threshold": "2000"
}
]
}
离我最近的是
.results
| map(.Threshold //= "0")
| .[]
| select((.Threshold|tonumber > 0) and
(.Diff|tonumber > .Threshold|tonumber))
但这给出了一个
Cannot index string with string "Threshold"
错误。
基本上我想返回 Diff 大于非零阈值的所有结果。所以在这种情况下:
{
"A": "moo",
"B": "goo",
"Diff": "5000",
"Threshold": "2000"
}
使用 jq 1.5 FWIW。
【问题讨论】: