jq - 合并两个JSON文件时出错“无法成倍”

时间:2016-04-27 05:17:44

标签: json jq

我有两个包含一些JSON的文件。

File1 - 某些值可以是字符串,有些可能为null,VolumeId始终设置。

[
  {
    "VolumeId": "vol-xxxxxxx1",
    "Tag1": "Tag1Value",
    "Tag2": "Tag2Value",
    "Tag3": "Tag3Value",
    "Tag4": "Tag4Value",
    "Tag5": "Tag5Value",
    "Tag6": "Tag6Value"
  },
  {
    "VolumeId": "vol-xxxxxxx2",
    "Tag1": "Tag1Value",
    "Tag2": "null",
    "Tag3": "null",
    "Tag4": "Tag4Value",
    "Tag5": "null",
    "Tag6": "Tag6Value"
  },
  {
    "VolumeId": "vol-xxxxxxx3",
    "Tag1": "null",
    "Tag2": "null",
    "Tag3": "null",
    "Tag4": "null",
    "Tag5": "null",
    "Tag6": "null"
  }
]

File2 - VolumeId将始终与File1中的匹配,Tag7可能为空,也可能不为。

[
  {
    "VolumeId": "vol-xxxxxxx1",
    "Tag7": "Tag7Value"
  },
  {
    "VolumeId": "vol-xxxxxxx2",
    "Tag7": "Tag7Value"
  },
  {
    "VolumeId": "vol-xxxxxxx3",
    "Tag7": "null"
  }
]

我需要将它们组合成一个JSON数组:

[
  {
    "VolumeId": "vol-xxxxxxx1",
    "Tag1": "Tag1Value",
    "Tag2": "Tag2Value",
    "Tag3": "Tag3Value",
    "Tag4": "Tag4Value",
    "Tag5": "Tag5Value",
    "Tag6": "Tag6Value",
    "Tag7": "Tag7Value"
  },
  {
    "VolumeId": "vol-xxxxxxx2",
    "Tag1": "Tag1Value",
    "Tag2": "null",
    "Tag3": "null",
    "Tag4": "Tag4Value",
    "Tag5": "null",
    "Tag6": "Tag6Value",
    "Tag7": "Tag7Value"
  },
  {
    "VolumeId": "vol-xxxxxxx3",
    "Tag1": "null",
    "Tag2": "null",
    "Tag3": "null",
    "Tag4": "null",
    "Tag5": "null",
    "Tag6": "null",
    "Tag7": "null"
  }
]

尝试使用以下命令时出错:

jq -s '.[0] * .[1]' /path/to/file1 /path/to/file2
array ([{"VolumeId...) and array ([{"VolumeId...) cannot be multiplied

感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

我假设您要将具有相同.VolumeId的所有条目合并为一个。为此,首先我们将flatten对象数组数组放入一个对象数组中。然后,我们group_by(.VolumeId)。最后,我们map对每个组reduce进行计算,并计算其所有元素的乘积。最终结果如下:

jq -s 'flatten | group_by(.VolumeId) | map(reduce .[] as $x ({}; . * $x))' /path/to/file1 /path/to/file2

答案 1 :(得分:0)

你应该做什么:

<强> File1中

{
  "vol-xxxxxxx1": {
    "Tag1": "Tag1Value",
    "Tag2": "Tag2Value",
    "Tag3": "Tag3Value",
    "Tag4": "Tag4Value",
    "Tag5": "Tag5Value",
    "Tag6": "Tag6Value"
  },
  "vol-xxxxxxx2": {
    "Tag1": "Tag1Value",
    "Tag2": "null",
    "Tag3": "null",
    "Tag4": "Tag4Value",
    "Tag5": "null",
    "Tag6": "Tag6Value"
  },
  "vol-xxxxxxx3": {
    "Tag1": "null",
    "Tag2": "null",
    "Tag3": "null",
    "Tag4": "null",
    "Tag5": "null",
    "Tag6": "null"
  }
}

<强>文件2

{
  "vol-xxxxxxx1": {
    "Tag7": "Tag7Value"
  },
  "vol-xxxxxxx2": {
    "Tag7": "Tag7Value"
  },
  "vol-xxxxxxx3": {
    "Tag7": "null"
  }
}

现在命令

jq -s '.[0] * .[1]' /path/to/file1 /path/to/file2

给出以下内容:

{
  "vol-xxxxxxx1": {
    "Tag1": "Tag1Value",
    "Tag2": "Tag2Value",
    "Tag3": "Tag3Value",
    "Tag4": "Tag4Value",
    "Tag5": "Tag5Value",
    "Tag6": "Tag6Value",
    "Tag7": "Tag7Value"
  },
  "vol-xxxxxxx2": {
    "Tag1": "Tag1Value",
    "Tag2": "null",
    "Tag3": "null",
    "Tag4": "Tag4Value",
    "Tag5": "null",
    "Tag6": "Tag6Value",
    "Tag7": "Tag7Value"
  },
  "vol-xxxxxxx3": {
    "Tag1": "null",
    "Tag2": "null",
    "Tag3": "null",
    "Tag4": "null",
    "Tag5": "null",
    "Tag6": "null",
    "Tag7": "null"
  }
}