【问题标题】:`in` operator in jq. But, it doesn't work as I wantjq 中的 `in` 运算符。但是,它不像我想要的那样工作
【发布时间】:2021-10-12 18:26:28
【问题描述】:

输入如下:

{"type": "mammal", "value": "lion"}
{"type": "reptile", "value": "snake"}
{"type": "insect", "value": "fly"}
{"type": "mammal", "value": "chicken"}

我可以通过select(.type=="mammal" or .type=="reptile") 得到我想要的。但这是简化版,正如您所料,这可能太冗长了。

所以我想用更简洁的方式做同样的事情,比如select(.type in ["mammal", "reptile"])。我知道它是无效的,它是一个伪代码,你可以理解我的意图。

我用 in/1 和 index/1 尝试了几件事,但我不高兴……我该怎么办?

【问题讨论】:

    标签: json shell command-line jq


    【解决方案1】:

    这是你要找的吗?

    select(.type | IN("mammal","reptile"))
    

    【讨论】:

    • 什么..?!没门..!今天我了解到在 jq 中除了 in 之外还有一个 IN 运算符,是的,这正是我想要的。感谢您的帮助
    • 我也不知道IN。它是“相对”新的,已在最新版本中引入(1.6,但仍有近 3 年的历史。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多