Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.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
使用jq添加到现有的json文件_Json_Bash_Loops_Jq_Artifactory Query Lang - Fatal编程技术网

使用jq添加到现有的json文件

使用jq添加到现有的json文件,json,bash,loops,jq,artifactory-query-lang,Json,Bash,Loops,Jq,Artifactory Query Lang,我有一个JSON格式的规范文件。等级库文件如下所示: { "files": [ { "aql": { "items.find": { "repo": "release-repo", "modified": { "$before": "30d" }, "type": { "$eq": "folder" }, "depth": "2" } } }

我有一个JSON格式的规范文件。等级库文件如下所示:

{
  "files": [
    {
      "aql": {
        "items.find": {
          "repo": "release-repo",
          "modified": { "$before": "30d" },
          "type": { "$eq": "folder" },
          "depth": "2"
        }
      }
    }
  ]
}
假设我运行一个gitlab api查询来获取我想要迭代并添加到这个json规范文件中的SHA列表。。SHA列表被分配给一个变量

"a991fef6bb9e9759d513fd4b277fe3674b44e4f4"
"5a562d34bb1d4ab4264acc2c61327651218524ad"
"d4e296c35644743e58aed35d1afb87e34d6c8823"
我希望遍历中的所有这些提交ID,并将它们逐个添加到json中,以便它们采用以下格式:

{
  "files": [
    {
      "aql": {
        "items.find": {
          "repo": "release-repo",
          "modified": { "$before": "30d" },
          "type": { "$eq": "folder" },
          "$or": [
            {
              "$and": [
                {
                  "name": {
                    "$nmatch": "*a991fef6bb9e9759d513fd4b277fe3674b44e4f4*"
                  }
                }
              ]
            },
            {
              "$and": [
                {
                  "name": {
                    "$nmatch": "*5a562d34bb1d4ab4264acc2c61327651218524ad*"
                  }
                }
              ]
            },
            {
              "$and": [
                {
                  "name": {
                    "$nmatch": "*d4e296c35644743e58aed35d1afb87e34d6c8823*"
                  }
                }
              ]
            }
          ],
          "depth": "2"
        }
      }
    }
  ]
}
从gitlab api查询返回的SHA列表将完全不同,这就是为什么每次我都希望它是一个动态条目或更新。返回的SHA数量也将不同。。。一天可以返回10,另一天可以返回50

#!/usr/bin/env bash

template='{
  "files": [
    {
      "aql": {
        "items.find": {
          "repo": "release-repo",
          "modified": { "$before": "30d" },
          "type": { "$eq": "folder" },
          "$or": [],
          "depth": "2"
        }
      }
    }
  ]
}'

shas=(
  "a991fef6bb9e9759d513fd4b277fe3674b44e4f4"
  "5a562d34bb1d4ab4264acc2c61327651218524ad"
  "d4e296c35644743e58aed35d1afb87e34d6c8823"
)

jq -n \
        --argjson template "$template" \
        --arg shas_str "${shas[*]}" \
'
reduce ($shas_str | split(" ") | .[]) as $sha ($template;
  .files[0].aql["items.find"]["$or"] += [{
    "$and": [{"name": {"$nmatch": ("*" + $sha + "*")}}]
  }]
)
'
…作为输出发射:

{
  "files": [
    {
      "aql": {
        "items.find": {
          "repo": "release-repo",
          "modified": {
            "$before": "30d"
          },
          "type": {
            "$eq": "folder"
          },
          "$or": [
            {
              "$and": [
                {
                  "name": {
                    "$nmatch": "*a991fef6bb9e9759d513fd4b277fe3674b44e4f4*"
                  }
                }
              ]
            },
            {
              "$and": [
                {
                  "name": {
                    "$nmatch": "*5a562d34bb1d4ab4264acc2c61327651218524ad*"
                  }
                }
              ]
            },
            {
              "$and": [
                {
                  "name": {
                    "$nmatch": "*d4e296c35644743e58aed35d1afb87e34d6c8823*"
                  }
                }
              ]
            }
          ],
          "depth": "2"
        }
      }
    }
  ]
}

这是一个不使用reduce的解决方案。它做出了一些无关紧要的假设- sha字符串在STDIN上显示为字符串流,并且Artifactory规范位于名为spec.json的文件中。以下是jq计划:

map( {"$and": [ {name: { "$nmatch": "*\(.)*" }}]} ) as $x
| $spec[0] | (.files[0].aql."items.find"."$or" = $x)
jq调用可能如下所示:

jq -s --slurpfile spec spec.json -f program.jq <<< "${shas[*]}"

jq-s--slurpfile spec.json-f program.jq您的shell变量中有引号吗?(其具体格式是什么?@CharlesDuffy将变量设置为:export result=$(curl--header“PRIVATE-TOKEN:*************”“”| jq.[]| select(.name | contains(“release”))|.commit.id'),返回:echo$results“a991fef6bb9e9759d513fd4b277fe3674b4e4f4”“5a562d344ab4acc61327651218524ad”“D4E96C3562473E58AED35D1AFB8E24D6C823”(实际上我在回答中假定它是一个shell数组,并且引号是语法而不是文字的)。@ CalsLeDuffy,我可以更新命令,从SASS <代码>列表中删除引号。(它的输出在许多方面都是不明确的)。请改用
declare-p results