繁体   English   中英

使用 jq 根据输入中其他地方的值从数组中删除项目

[英]Using jq to remove items from an array based on values elsewhere in the input

我是jq新手,并试图围绕做一些我认为很简单的事情。 任何关于如何改进这一点的想法将不胜感激。

鉴于此输入:

{
    "source": {
        "items": [
            { "id": "aaa", "name": "this" },
            { "id": "bbb", "name": "that" },
            { "id": "ccc", "name": "the other" }
        ]
    },
    "update": {
        "toRemove": [
            "aaa",
            "ccc"
        ]
    }
}

我想要这个结果:

{
    "items": [
        { "id": "bbb", "name": "that" }
    ]
}

这个过滤器可以完成这项工作,但变量让我相信有一种更清洁的方法。

. as $root | .source + { items: [.source.items[] | select(.id as $id | $root.update.toRemove | contains([$id]) | not)]}

如果有兴趣,游乐场链接: https : //jqplay.org/s/GpVJfbTO-Q

这是一个简洁而有效的解决方案:

.update.toRemove as $rm
| .source
| .items |= map( select(.id | IN($rm[]) | not))

使用inside而不是contains更短的版本:

.update.toRemove as $temp |
 {items: [.source.items[] | select([.id] | inside($temp) | not)]}

和另一种解决方案,使用jtc

bash $ <input.json jtc -w'<items>l[-1]' | jtc -w'[id]:<bbb>[-1]' -pp
{
   "items": [
      {
         "id": "bbb",
         "name": "that"
      }
   ]
}
bash $ 

第一个jtc语句完全打印items ,第二个删除除了bbb对象以外的所有内容

* UPDATE *最新版本的jtc现在允许名称空间。 为方便询问,请使用如下:

bash $ <input.json jtc -w'[update][toRemove][:]<2rm>v [^0][id]:<2rm>s[-1]' -p | jtc -w'[source]'
{
   "items": [
      {
         "id": "bbb",
         "name": "that"
      }
   ]
}
bash $ 

说明:

1. [update][toRemove][:]<2rm>v - 步行路径的那一部分将迭代toRemove每个项目,同时将每个迭代值存储在命名空间2rm

2. [^0][id]:<2rm>s[-1] - 将步行路径重置回root( [^0] )并找到与2rm存储的匹配的每个JSON条目, [-1]选择找到的条目的父级(选择项目记录

3. -p将清除找到的(走过的)元素,然后jtc将选择source

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM