用jq(cmdline)添加json数组元素 [英] Add json array element with jq (cmdline)

查看:274
本文介绍了用jq(cmdline)添加json数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

使用以下过滤器

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

但是我想拥有

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

jq/bash有办法吗?

Is there a way with jq/bash?

推荐答案

所以,我在这里暗中刺了一下(混合隐喻),但这似乎是您想要的结果:

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

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

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

这将产生:

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

然后我们得到:

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

这篇关于用jq(cmdline)添加json数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆