jq - 像数组一样遍历 bash 中的对象(aws 卷)
jq - Iterate over objects in bash like an array (aws volumes)
我从 AWS 中提取了一些 JSON 并使用 jq 格式化(原始代码在底部),得到以下输出:
{
"VolumeId": "vol-11111111",
"Tags": {
"Name": "volume1",
"Finish": "00:00",
"Start": "00:20",
"Period": "2"
}
}
{
"VolumeId": "vol-22222222",
"Tags": {
"Name": "volume2",
"Period": "1",
"Start": "00:00",
"Finish": "00:20"
}
}
{
"VolumeId": "vol-33333333",
"Tags": {
"Period": "1",
"Start": "00:00",
"Name": "volume3",
"Finish": "00:20"
}
}
我现在需要做的是拉取'VolumeId'、'Period'、'Start'和'Finish'。我想遍历这些对象,将它们放入 for
循环中的 4 个 bash 同名变量中。
例如
VolumeId="vol-33333333"
Period="1"
Start="00:00"
Finish="00:20"
问题是,如果我将整个 JSON 放入一个变量中,它将被视为单个参数。我可以使用 mapfile
之类的东西,但是它会把它变成太多参数 - 例如
}
"Volumes": [
{
等等
如果您能帮助我们实现这一点,我们将不胜感激。最终结果是能够拍摄卷的快照并使用 'Period' 标签来计算保留等
--
原文JSON:
{
"Volumes": [
{
"Attachments": [],
"Tags": [
{
"Value": "volume1",
"Key": "Name"
},
{
"Value": "00:00",
"Key": "Start"
},
{
"Value": "00:20",
"Key": "Finish"
},
{
"Value": "2",
"Key": "Period"
}
],
"VolumeId": "vol-11111111"
},
{
"Attachments": [],
"Tags": [
{
"Value": "volume2",
"Key": "Name"
},
{
"Value": "00:00",
"Key": "Start"
},
{
"Value": "00:20",
"Key": "Finish"
},
{
"Value": "2",
"Key": "Period"
}
],
"VolumeId": "vol-22222222"
},
{
"Attachments": [],
"Tags": [
{
"Value": "volume3",
"Key": "Name"
},
{
"Value": "00:00",
"Key": "Start"
},
{
"Value": "00:20",
"Key": "Finish"
},
{
"Value": "2",
"Key": "Period"
}
],
"VolumeId": "vol-33333333"
}
]
}
和jq命令:
jq -r '.Volumes[] | {"VolumeId": .VolumeId, "Tags": [.Tags[]] | from_entries}'
cat rawjsonfile |jq -r '.Volumes[]|({VolumeId}+(.Tags|from_entries))|{VolumeId,Period,Start,Finish}|to_entries[]|(.key+"="+.value)'
原始json文件是您的“-- 原始 JSON”
这个结果是:
VolumeId=vol-11111111
Period=2
Start=00:00
Finish=00:20
VolumeId=vol-22222222
Period=2
Start=00:00
Finish=00:20
VolumeId=vol-33333333
Period=2
Start=00:00
Finish=00:20
- 首先将数组展开到 json 个单位
cat rawjsonfile|jq -r '.Volumes[]|({VolumeId}+(.Tags|from_entries))'
第一步的结果是这样的:
{
"VolumeId": "vol-11111111",
"Name": "volume1",
"Start": "00:00",
"Finish": "00:20",
"Period": "2"
}
{
"VolumeId": "vol-22222222",
"Name": "volume2",
"Start": "00:00",
"Finish": "00:20",
"Period": "2"
}
{
"VolumeId": "vol-33333333",
"Name": "volume3",
"Start": "00:00",
"Finish": "00:20",
"Period": "2"
}
jq支持加入json对象。
- 其次选择字段
|{VolumeId,Period,Start,Finish}
3.make 它到键值并加入它们
|to_entries[]|(.key+"="+.value)
我从 AWS 中提取了一些 JSON 并使用 jq 格式化(原始代码在底部),得到以下输出:
{
"VolumeId": "vol-11111111",
"Tags": {
"Name": "volume1",
"Finish": "00:00",
"Start": "00:20",
"Period": "2"
}
}
{
"VolumeId": "vol-22222222",
"Tags": {
"Name": "volume2",
"Period": "1",
"Start": "00:00",
"Finish": "00:20"
}
}
{
"VolumeId": "vol-33333333",
"Tags": {
"Period": "1",
"Start": "00:00",
"Name": "volume3",
"Finish": "00:20"
}
}
我现在需要做的是拉取'VolumeId'、'Period'、'Start'和'Finish'。我想遍历这些对象,将它们放入 for
循环中的 4 个 bash 同名变量中。
例如
VolumeId="vol-33333333"
Period="1"
Start="00:00"
Finish="00:20"
问题是,如果我将整个 JSON 放入一个变量中,它将被视为单个参数。我可以使用 mapfile
之类的东西,但是它会把它变成太多参数 - 例如
}
"Volumes": [
{
等等
如果您能帮助我们实现这一点,我们将不胜感激。最终结果是能够拍摄卷的快照并使用 'Period' 标签来计算保留等
-- 原文JSON:
{
"Volumes": [
{
"Attachments": [],
"Tags": [
{
"Value": "volume1",
"Key": "Name"
},
{
"Value": "00:00",
"Key": "Start"
},
{
"Value": "00:20",
"Key": "Finish"
},
{
"Value": "2",
"Key": "Period"
}
],
"VolumeId": "vol-11111111"
},
{
"Attachments": [],
"Tags": [
{
"Value": "volume2",
"Key": "Name"
},
{
"Value": "00:00",
"Key": "Start"
},
{
"Value": "00:20",
"Key": "Finish"
},
{
"Value": "2",
"Key": "Period"
}
],
"VolumeId": "vol-22222222"
},
{
"Attachments": [],
"Tags": [
{
"Value": "volume3",
"Key": "Name"
},
{
"Value": "00:00",
"Key": "Start"
},
{
"Value": "00:20",
"Key": "Finish"
},
{
"Value": "2",
"Key": "Period"
}
],
"VolumeId": "vol-33333333"
}
]
}
和jq命令:
jq -r '.Volumes[] | {"VolumeId": .VolumeId, "Tags": [.Tags[]] | from_entries}'
cat rawjsonfile |jq -r '.Volumes[]|({VolumeId}+(.Tags|from_entries))|{VolumeId,Period,Start,Finish}|to_entries[]|(.key+"="+.value)'
原始json文件是您的“-- 原始 JSON”
这个结果是:
VolumeId=vol-11111111
Period=2
Start=00:00
Finish=00:20
VolumeId=vol-22222222
Period=2
Start=00:00
Finish=00:20
VolumeId=vol-33333333
Period=2
Start=00:00
Finish=00:20
- 首先将数组展开到 json 个单位
cat rawjsonfile|jq -r '.Volumes[]|({VolumeId}+(.Tags|from_entries))'
第一步的结果是这样的:
{
"VolumeId": "vol-11111111",
"Name": "volume1",
"Start": "00:00",
"Finish": "00:20",
"Period": "2"
}
{
"VolumeId": "vol-22222222",
"Name": "volume2",
"Start": "00:00",
"Finish": "00:20",
"Period": "2"
}
{
"VolumeId": "vol-33333333",
"Name": "volume3",
"Start": "00:00",
"Finish": "00:20",
"Period": "2"
}
jq支持加入json对象。
- 其次选择字段
|{VolumeId,Period,Start,Finish}
3.make 它到键值并加入它们
|to_entries[]|(.key+"="+.value)