2

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

我试图通过 jq 之类的东西,.[].run.command但我不断收到错误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"
    }
  }
]
4

2 回答 2

2

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

.[].run?.command

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

这是另一个变体,它暗示了其他变体:

.[] | objects | .run.command
于 2018-11-01T17:16:16.853 回答
2

如果您正在处理具有固定结构的这类数组的集合,其中第一项就像一个别名,然后是一堆动作,您可以跳过第一项并处理其余的。

.[1:][].run.command

否则,如果它真的是混合的并且任何项目都可以是任何东西,则必须进行某种过滤作为峰值轮廓。

于 2018-11-01T18:17:04.807 回答