Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何使用jq过滤具有混合类型元素的数组_Json_Jq - Fatal编程技术网

Json 如何使用jq过滤具有混合类型元素的数组

Json 如何使用jq过滤具有混合类型元素的数组,json,jq,Json,Jq,给定下面的JSON,我将尝试选择所有命令元素。我的问题是“checkout”字符串把我选择的所有“run”指令都弄乱了 我试图传递jq类似于[].run.command的内容,但我不断收到错误jq:error(at:33):无法使用字符串“run”索引字符串 如何排除选中的“checkout”字符串 [ "checkout", { "run": { "command": "pip install cfn-lint pycodestyle pyflakes",

给定下面的JSON,我将尝试选择所有
命令
元素。我的问题是
“checkout”
字符串把我选择的所有
“run”
指令都弄乱了

我试图传递jq类似于
[].run.command
的内容,但我不断收到错误
jq:error(at:33):无法使用字符串“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

如果您处理的是一组具有固定结构的数组,其中第一项类似于一个别名,后面跟着一系列操作,那么您可以跳过第一项,然后处理其余项

.[1:][].run.command
否则,如果它是真正的混合,任何项目可以是任何东西,某种过滤将不得不做为峰值轮廓