jq - 将其他JSON对象提取到新数组

时间:2016-04-04 02:39:54

标签: json jq

我有一些JSON。它看起来像这样:

{
"Volumes": [
    {
        "Attachments": [
            {
                "VolumeId": "vol-11111111",
                "State": "attached",
                "DeleteOnTermination": false,
                "Device": "/dev/sdz"
            }
        ],
        "Tags": [
            {
                "Value": "volume1",
                "Key": "Name"
            },
            {
                "Value": "00:00",
                "Key": "Start"
            },
            {
                "Value": "00:20",
                "Key": "Finish"
            },
            {
                "Value": "2",
                "Key": "Period"
            }
        ],
        "VolumeId": "vol-11111111"
    },
    {
        "Attachments": [
            {
                "VolumeId": "vol-22222222",
                "State": "attached",
                "DeleteOnTermination": false,
                "Device": "/dev/sdz"
            }
        ],
        "Tags": [
            {
                "Value": "volume2",
                "Key": "Name"
            },
            {
                "Value": "00:00",
                "Key": "Start"
            },
            {
                "Value": "00:20",
                "Key": "Finish"
            },
            {
                "Value": "2",
                "Key": "Period"
            }
        ],
        "VolumeId": "vol-22222222"
    },
    {
        "Attachments": [
            {
                "VolumeId": "vol-333333333",
                "State": "attached",
                "DeleteOnTermination": false,
                "Device": "/dev/sdz"
            }
        ],
        "Tags": [
            {
                "Value": "volume3",
                "Key": "Name"
            },
            {
                "Value": "00:00",
                "Key": "Start"
            },
            {
                "Value": "00:20",
                "Key": "Finish"
            },
            {
                "Value": "2",
                "Key": "Period"
            }
        ],
        "VolumeId": "vol-33333333"
    }
]
}

使用jq,我可以提取以下信息:

VolumeId,Finish,Start,Period

使用jq命令

cat json | jq -r '[.Volumes[]|({VolumeId}+(.Tags|from_entries))|{VolumeId,Finish,Start,Period}]'

[
  {
    "VolumeId": "vol-11111111",
    "Finish": "00:20",
    "Start": "00:00",
    "Period": "2"
  },
  {
    "VolumeId": "vol-22222222",
    "Finish": "00:20",
    "Start": "00:00",
    "Period": "2"
  },
  {
    "VolumeId": "vol-33333333",
    "Finish": "00:20",
    "Start": "00:00",
    "Period": "2"
  }
]

这一切都很好。但是我需要额外的提取.Attachments.Device。我正在寻找类似于:

的每个数组的输出
 [
  {
    "VolumeId": "vol-11111111",
    "Finish": "00:20",
    "Start": "00:00",
    "Period": "2",
    "DeviceId": "/dev/sdz"
  },
  {
    "VolumeId": "vol-22222222",
    "Finish": "00:20",
    "Start": "00:00",
    "Period": "2",
    "DeviceId": "/dev/sdz"
  },
  {
    "VolumeId": "vol-33333333",
    "Finish": "00:20",
    "Start": "00:00",
    "Period": "2",
    "DeviceId": "/dev/sdz"
  }
]

但是,如果不出错,我无法弄清楚如何做到这一点。对我来说最合乎逻辑的方法是做一些事情:

cat json | jq -r '[.Volumes[]|({VolumeId}+(.Attachments|from_entries)+(.Tags|from_entries))|{VolumeId,Finish,Start,Period,DeviceId}]'

然而我收到错误:

jq: error (at <stdin>:91): Cannot use null (null) as object key

任何帮助弄清楚我做的不正确以及如何解决它将非常感激。

感谢

1 个答案:

答案 0 :(得分:2)

最终,问题是您在from_entries数组上使用Attachments时无法正常工作。 from_entries使用一组键/值对对象来创建具有这些值的对象。但是,您没有键/值对,而是对象。如果您只想尝试将它们合并,则应使用add

此外,没有名为DeviceId的属性,它是Device。如果您要选择Device属性并将其设为DeviceId,则需要提供正确的名称。

.Volumes | map(
    ({ VolumeId } + (.Attachments | add) + (.Tags | from_entries))
      | { VolumeId, Finish, Start, Period, DeviceId: .Device }
)