繁体   English   中英

如何使用 jq 删除具有包含特定替换的键的元素?

[英]How to delete an element which has a key which contains a certain substing using jq?

我有以下 JSON:

{
  "Category": [
    {
      "SomeKey_1": {
        "Property1": 1,
        "Property2": false
      }
    },
    {
      "SomeKey_2": {
        "Property1": 2,
        "Property2": true
      }
    },
    {
      "OtherKey_1": {
        "Property1": 3,
        "Property2": false
      }
    },
    {
      "OtherKey_2": {
        "Property1": 4,
        "Property2": false
      }
    }
  ]
}

我想从 Category[] 数组中删除那些键以“Other”开头或包含“Other”的元素。 所以结果我想要这个:

{
  "Category": [
    {
      "SomeKey_1": {
        "Property1": 1,
        "Property2": false
      }
    },
    {
      "SomeKey_2": {
        "Property1": 2,
        "Property2": true
      }
    }
  ]
}

我尝试使用 Select 命令,但我可以根据值而不是键使用 select。

如果数组中的所有对象只包含一个属性:

.Category |= map(select(keys_unsorted[0] | contains("Other") | not))

如果要删除任何属性键以“其他”开头的对象:

.Category |= map(select(any(keys_unsorted[]; contains("Other")) | not))

最后,如果您只想删除那些只有“其他”属性的对象:

.Category |= map(select(all(keys_unsorted[]; contains("Other")) | not))

也可以使用del过滤器:

del(.Category[] | select(keys_unsorted[0] | contains("Other")))
del(.Category[] | select(any(keys_unsorted[]; contains("Other"))))
del(.Category[] | select(all(keys_unsorted[]; contains("Other"))))

暂无
暂无

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

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