如何使其与 jq 中的过滤器一起使用

How to make it work with filter in jq

我想过滤 json 文件下方的输出,以从 "tag_Name"

开始
{
  ...

  "tag_Name_abc": [
    "10_1_4_3",
    "10_1_6_2",
    "10_1_5_3",
    "10_1_5_5"
  ],
  "tag_Name_efg": [
    "10_1_4_5"
  ],

  ...

}

尝试了一些但失败了。

$ cat output.json |jq 'map(select(startswith("tag_Name")))'
jq: error (at <stdin>:1466): startswith() requires string inputs

有很多方法可以做到这一点,但最简单的方法是将该对象转换为条目,这样您就可以访问密钥,然后按您想要的名称过滤条目,然后再返回。

with_entries(select(.key | startswith("tag_Name")))

这里还有几个解决方案:

1) 将匹配键的值与 add

相结合
   . as $d
 | keys
 | map( select(startswith("tag_Name")) | {(.): $d[.]} )
 | add

2) 用 delpaths

过滤掉不匹配的键
 delpaths([
      keys[]
    | select(startswith("tag_Name") | not)
    | [.]
 ])

3) 用 reducedel

过滤掉不匹配的键
 reduce keys[] as $k (
   .
 ; if ($k|startswith("tag_Name")) then . else del(.[$k]) end
 )