我试着用JMESPath做一个简单的if/then/else
例如:‘如果输入是字符串,则返回该字符串,否则返回输入的"value“属性’。"abc"的输入将返回"abc"。{"value":"def"}的输入将返回"def"
有了jq,这很简单:if .|type == "string" then . else .value end
使用JMESPath,我可以获取类型
type(@)
或者输入:
@
或value属性:
value
但是我还没有找到一种将它们组合成if-then-else的方法。有没有办法做到这一点?
发布于 2021-11-25 00:21:11
这是可能的,但不是一目了然的。一般的形式是:
这应该允许您为false和true条件派生可能的转换。
例如,如果测试数据为:
{
"test": 11
}根据获得的值,您可以生成结果(以测试数据11和2为例):
如下所示:
[
map(
&join(' ', ['Yes, the value is', to_string(@), 'which is greater than 10']),
[test][? @ > `10`]
),
join(' ', ['No the value is', to_string(test), ' which is less than or equal to 10'])
][] | @[0]因此,要抽象一个模板:
[
map(
&<True Expression Here>,
[<Expression you are testing>][? @ <Test Expression>]
),
<False Expression Here>)
][] | @[0]发布于 2021-03-28 14:10:57
人员[?general.id !=**100**] || people
{
"people": [
{
"general": {
"id": 100,
"age": 20,
"other": "foo",
"name": "Bob"
},
"history": {
"first_login": "2014-01-01",
"last_login": "2014-01-02"
}
},
{
"general": {
"id": 101,
"age": 30,
"other": "bar",
"name": "Bill"
},
"history": {
"first_login": "2014-05-01",
"last_login": "2014-05-02"
}
}
]
}if else条件在此处有效
https://stackoverflow.com/questions/64308650
复制相似问题