jq-将其他JSON对象提取到新数组中 [英] jq - extract additional JSON object into new array
问题描述
我有一些JSON.看起来像这样:
I have some JSON. It looks like this:
{
"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,我可以提取以下信息:
Using jq, I am able to extract the following information:
VolumeId,完成,开始,句点
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.我正在寻找类似于以下每个数组的输出:
All this works fine. However I have the need to additional extract .Attachments.Device. I am looking for output for each array similar to:
[
{
"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"
}
]
但是,我无法弄清楚如何做到这一点而不会出错.对我而言,最合乎逻辑的方法是执行以下操作:
However I can't figure out how to do this without getting an error. The most logical approach for me would be to do something like:
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
任何帮助弄清楚我在做什么不正确以及如何解决它,将不胜感激.
Any help figuring out what I am not doing correct and how to fix it would be greatly appreciated.
谢谢
推荐答案
最终,问题在于您无法在Attachments
数组上使用from_entries
. from_entries
使用键/值对对象数组来创建具有这些值的对象.但是,您没有键/值对,但是有对象.如果您只是尝试将它们组合,则应使用add
.
Ultimately, the problem is that you're using from_entries
on the Attachments
array when it wouldn't work. from_entries
takes an array of key/value pair objects to create an object with those values. However, you don't have key/value pairs, but objects. If you're just trying to combine them, you should use add
.
此外,没有名为DeviceId
的属性,它是Device
.如果要选择Device
属性并将其获取为DeviceId
,则需要提供正确的名称.
Also, there is no property named DeviceId
, it's Device
. If you want to select the Device
property and get it as DeviceId
, you need to provide the correct name.
.Volumes | map(
({ VolumeId } + (.Attachments | add) + (.Tags | from_entries))
| { VolumeId, Finish, Start, Period, DeviceId: .Device }
)
这篇关于jq-将其他JSON对象提取到新数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!