简体   繁体   English

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

[英]Add to existing json file using jq

I have an Artifactory AQL Spec file in JSON format. 我有一个JSON格式的Artifactory AQL Spec文件。 The spec file is as follows: spec文件如下:

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

let's say I run a gitlab api query to acquire a list of SHAs that I want to iterate through and add to this json spec file.. The list of SHAs are assigned to a variable.. 假设我运行一个gitlab api查询来获取我想要迭代的SHA列表并添加到这个json spec文件中.SHA列表被分配给一个变量..

"a991fef6bb9e9759d513fd4b277fe3674b44e4f4"
"5a562d34bb1d4ab4264acc2c61327651218524ad"
"d4e296c35644743e58aed35d1afb87e34d6c8823"

I would like to iterate through all these commit IDs in and add them one by one to the json so that they are in this format: 我想迭代所有这些提交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"
        }
      }
    }
  ]
}

The list of SHAs returned from the gitlab api query will be different everything and that's why I'd like this to be a dynamic entry or update every time. 从gitlab api查询返回的SHA列表将是不同的,这就是为什么我希望每次都是动态条目或更新。 The number of returned SHAs will also be different... Could return 10 one day or it could return 50 on another day. 返回的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 + "*")}}]
  }]
)
'

...emits as output: ...作为输出发出:

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

Here is a reduce-free solution. 这是一个免费的解决方案。 It makes some inessential assumptions - that the sha strings are presented as a stream of strings on STDIN, and that the Artifactory spec is in a file named spec.json. 它做了一些不必要的假设 - sha字符串在STDIN上呈现为字符串流,并且Artifactory规范位于名为spec.json的文件中。 Here is the jq program: 这是jq程序:

map( {"$and": [ {name: { "$nmatch": "*\(.)*" }}]} ) as $x
| $spec[0] | (.files[0].aql."items.find"."$or" = $x)

The jq invocation might look like this: jq调用可能如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM