gpt4 book ai didi

json - 如何使用jq过滤包含混合类型元素的数组

转载 作者:行者123 更新时间:2023-12-05 02:14:45 36 4
gpt4 key购买 nike

鉴于以下 JSON,我正在尝试选择所有 command元素。我的问题是 "checkout"字符串弄乱了我对所有 "run" 的选择听写。

我正在尝试传递类似 .[].run.command 的 jq但我不断收到错误 jq: error (at <stdin>:33): Cannot index string with string "run"

如何排除 "checkout"字符串被选中?

[
"checkout",
{
"run": {
"command": "pip install cfn-lint pycodestyle pyflakes",
"name": "Install Dependencies"
}
},
{
"run": {
"command": "find stacks -type f -name \"*.yml\" -exec cfn-lint -i E3016 -i E3012 -i E1029 --template {} \\; | tee -a output.txt",
"name": "Run cfn-lint"
}
},
{
"run": {
"command": "find stacks -type f -name \"*.py\" -exec pyflakes {} \\; | tee -a output.txt",
"name": "Run pyflakes"
}
},
{
"run": {
"command": "pycodestyle --show-source --show-pep8 --ignore=E501,W605 stacks/src | tee -a output.txt",
"name": "Run pycodestyle"
}
},
{
"run": {
"command": "if [[ -s output.txt ]]; then cat output.txt; exit 1; fi",
"name": "Exit if there are validation warnings/errors"
}
}
]

最佳答案

这是一个解决方案,它是您展示的过滤器的简单变体:

.[].run?.command

“?”是 try 包装器的语法糖。

这是另一种变体,暗示还有其他变体:

.[] | objects | .run.command

关于json - 如何使用jq过滤包含混合类型元素的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53105673/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com