bash 使用 jq (cmdline) 添加 json 数组元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25414854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:11:45  来源:igfitidea点击:

Add json array element with jq (cmdline)

bashubuntujq

提问by Layfon Weller

I'm trying to generate a json file within bash. I installed jq, hoping that it would help me generate and append json.

我正在尝试在 bash 中生成一个 json 文件。我安装了 jq,希望它能帮助我生成和附加 json。

For example, I want to generate a json in this format:

例如,我想以这种格式生成一个json:

{
  "Project": [
    {
      "projectName": {
        "branch": [
          {
            "branchName": [
              "path"
            ]
          }
        ],
        "tag": [
          {
            "tagName": [
              "path"
            ]
          }
        ]
      }
    }
  ]
}

While a can to something like that, with the following filter

虽然可以使用以下过滤器

 .Project=.Project+.Project+
  [{"projectName" : {"branch" : (.branch+[{"branchName":(.tagName+["path"])}]),
             "tag": (.tag+[{"tagName":(.tagName+["path"])}]) }}]

when I want to create another entry in the same project and name, it creates a whole new entry, has if it was a new project, resulting in this:

当我想在同一个项目和名称中创建另一个条目时,它会创建一个全新的条目,如果它是一个新项目,结果如下:

    {
      "Project": [
        {
          "projectName": {
            "branch": [
              {
                "branchName": [
                  "path"
                ]
              }
            ],
            "tag": [
              {
                "tagName": [
                  "path"
                ]
              }
            ]
          }
        },
        {
          "projectName": {
            "branch": [
              {
                "branchName": [
                  "path"
                ]
              }
            ],
            "tag": [
              {
                "tagName": [
                  "path"
                ]
              }
            ]
          }
        },
        {
          "projectName": {
            "branch": [
              {
                "branchName": [
                  "path2"
                ]
              }
            ],
            "tag": [
              {
                "tagName": [
                  "path2"
                ]
              }
            ]
          }
        }
      ]
    }

But I would like to have

但我想拥有

{
  "Project": [
    {
      "projectName": {
        "branch": [
          {
            "branchName": [
              "path",
              "path2"
            ]
          }
        ],
        "tag": [
          {
            "tagName": [
              "path",
              "path2"
            ]
          }
        ]
      }
    }
  ]
}

Is there a way with jq/bash?

jq/bash 有没有办法?

回答by zerodiff

So, I'm taking a stab in the dark here (to mix metaphors), but this gives what seems to be the results you want:

所以,我在这里暗中试探(混合隐喻),但这给出了你想要的结果:

cat test.json | jq '.Project[0].projectName.tag[0].tagName |= .+ ["path2"] | .Project[0].projectName.branch[0].branchName |= .+ ["path2"]'

The |= .+ [...]essentially appends a new array item. You can use the array specs for good effect for all array elements by omitting the 0from, e.g., tag[0].

|= .+ [...]本质上追加新的数组项。您可以通过省略0from,例如,对所有数组元素使用数组规范以获得良好的效果tag[0]

This produces:

这产生:

{
  "Project": [
    {
      "projectName": {
        "tag": [
          {
            "tagName": [
              "path",
              "path2"
            ]
          }
        ],
        "branch": [
          {
            "branchName": [
              "path",
              "path2"
            ]
          }
        ]
      }
    }
  ]
}

Edit -- if I understand the new method now (but I could be missing something), we start with:

编辑——如果我现在理解新方法(但我可能遗漏了一些东西),我们开始:

{
  "Project": {
    "projectName": {
      "tag": {
        "tagName": [
          "path",
        ]
      },
      "branch": {
        "branchName": [
          "path",
        ]
      }
    }
  }
}

Then set some variables and apply this transform:

然后设置一些变量并应用此转换:

Project=projectName ProjectNumber=path2 Branch=branchName Tag=tagName
jq ".Project.${Project}.tag.${Tag} |= .+ [\"${ProjectNumber}\"] | .Project.${Project}.branch.${Branch} |= .+ [\"${ProjectNumber}\"]"

And we get:

我们得到:

{
  "Project": {
    "projectName": {
      "tag": {
        "tagName": [
          "path",
          "path2"
        ]
      },
      "branch": {
        "branchName": [
          "path",
          "path2"
        ]
      }
    }
  }
}