使用JQ添加到现有的JSON文件中



我有一个以JSON格式的文物AQL规格文件。规格文件如下:

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

假设我运行了一个gitlab api查询,以获取我想迭代并添加到此JSON规格文件的SHA列表。.shas列表已分配给一个变量。

"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"
        }
      }
    }
  ]
}

这是一个无降压解决方案。它做出了一些不必要的假设 - SHA字符串作为stdin上的字符串流提供,并且伪影规范在名为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[*]}"

最新更新