jq select 比较对象中数组的两个最大值
jq select compare two max's of array within object
假设我必须遵循 JSON 文件:
{
"1.1.1.1": {
"history_ban": [
"2021-05-02 14:30",
"2022-01-01 12:00"
],
"history_unban": [
"2021-05-09 14:30",
"2022-01-08 12:00"
]
},
"2.2.2.2": {
"history_ban": [
"2022-01-16 07:00"
],
"history_unban": []
},
"3.3.3.3": {
"history_ban": [
"2022-01-15 22:40"
]
}
}
我的目标是获得所有钥匙,其中:
- 最大 history_ban 日期小于“2022-01-16 09:00”
- 最大 history_unban 日期 empty/non-existent 或小于最大 history_ban 日期
我相信我的大部分查询都按我想要的方式工作,但是 'Compare max unban with max ban' 没有工作。我当前(不工作)的查询如下:
to_entries[] | select((.value.history_ban != null) and (.value.history_ban | max < "2022-01-16 09:00") and ((.value.history_unban | length == 0 ) or (.value.history_unban | max < .value.history_ban | max))) | .key
我知道我的错误在 (.value.history_unban | max < .value.history_ban | max)
内,因为如果我用 (.value.history_unban | max < "somedate")
替换它,我会得到一个有效的查询。
我得到的错误是
jq: error (at :22): Cannot index array with string "value"
exit status 5
我需要select/compare这两个最大值做什么?
可以肯定的是,我在这个例子中的预期结果是
- "2.2.2.2"
- "3.3.3.3"
如果 history_unban
存在且不为空,您可以使用更新运算符 //
引入另一个约束。
jq -r '
to_entries[] | select(.value
| (.history_ban | max) as $maxban
| $maxban > "2022-01-16 09:00"
and (.history_unban | length == 0 // $maxban > max)
).key
'
2.2.2.2
3.3.3.3
假设我必须遵循 JSON 文件:
{
"1.1.1.1": {
"history_ban": [
"2021-05-02 14:30",
"2022-01-01 12:00"
],
"history_unban": [
"2021-05-09 14:30",
"2022-01-08 12:00"
]
},
"2.2.2.2": {
"history_ban": [
"2022-01-16 07:00"
],
"history_unban": []
},
"3.3.3.3": {
"history_ban": [
"2022-01-15 22:40"
]
}
}
我的目标是获得所有钥匙,其中:
- 最大 history_ban 日期小于“2022-01-16 09:00”
- 最大 history_unban 日期 empty/non-existent 或小于最大 history_ban 日期
我相信我的大部分查询都按我想要的方式工作,但是 'Compare max unban with max ban' 没有工作。我当前(不工作)的查询如下:
to_entries[] | select((.value.history_ban != null) and (.value.history_ban | max < "2022-01-16 09:00") and ((.value.history_unban | length == 0 ) or (.value.history_unban | max < .value.history_ban | max))) | .key
我知道我的错误在 (.value.history_unban | max < .value.history_ban | max)
内,因为如果我用 (.value.history_unban | max < "somedate")
替换它,我会得到一个有效的查询。
我得到的错误是
jq: error (at :22): Cannot index array with string "value" exit status 5
我需要select/compare这两个最大值做什么?
可以肯定的是,我在这个例子中的预期结果是
- "2.2.2.2"
- "3.3.3.3"
如果 history_unban
存在且不为空,您可以使用更新运算符 //
引入另一个约束。
jq -r '
to_entries[] | select(.value
| (.history_ban | max) as $maxban
| $maxban > "2022-01-16 09:00"
and (.history_unban | length == 0 // $maxban > max)
).key
'
2.2.2.2
3.3.3.3