排除输出的 jq 键。

huangapple go评论64阅读模式
英文:

jq exclude key for output

问题

{
"I need to exclude field _class from this output:": "我需要从这个输出中排除字段_class:",
"What I use for get this json": "我用什么来获取这个JSON",
"Result must be just:": "结果应该只是:",
"I tried using |not and del, but that didn't work for me.": "我尝试使用|not和del,但对我没有起作用。"
}

英文:

I need to exclude field _class from this output:

{
  "_class": "hudson.model.FreeStyleBuild",
  "displayName": "NAME-(#1498)",
  "number": 1498
}

What I use for get this json

curl -gn http://<jenkins-site>/job/<build-name>/api/json?tree=builds[displayName,number]&pretty=true | jq -r '.builds[] | select(.displayName | contains("NAME"))'

Result must be just:

{
  "displayName": "NAME-(#1498)",
  "number": 1498
}

I tried using |not and del, but that didn't work for me.

答案1

得分: 3

你可以使用 del 命令来简单地删除该字段:

… | jq '.builds[] | select(.displayName | contains("NAME")) | del(._class)'
{
  "displayName": "NAME-(#1498)",
  "number": 1498
}

注意:如果输出为 JSON 格式,-r 标志不起作用。

英文:

You can simply delete that field using del:

… | jq '.builds[] | select(.displayName | contains("NAME")) | del(._class)'
{
  "displayName": "NAME-(#1498)",
  "number": 1498
}

Note: If you output JSON, the -r flag isn't doing anything.

huangapple
  • 本文由 发表于 2023年3月7日 20:08:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661787.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定