用jq(命令行)添加json数组元素

时间:2014-08-20 21:37:31

标签: bash ubuntu jq

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

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

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

虽然可以使用以下过滤器

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

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

    {
      "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?

1 个答案:

答案 0 :(得分:13)

所以,我在黑暗中采取刺戳(混合隐喻),但这给出了你想要的结果:

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

|= .+ [...]实际上附加了一个新的数组项。通过省略0,例如tag[0],您可以使用数组规范对所有数组元素产生良好的效果。

这会产生:

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

编辑 - 如果我现在理解新方法(但我可能会遗漏某些东西),我们从:

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

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

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