bash - jq - 像数组一样遍历 bash 中的对象(aws 卷)

标签 bash jq

我有一些从 AWS 中提取并使用 jq 格式化的 JSON(原始代码在底部)以提供以下输出:

{
  "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)'

rawjsonfile 是您的“-- 原始 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
  1. 首先将数组展开为 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对象。

  1. 其次选择字段

|{VolumeId,Period,Start,Finish}

3.使其成为键值并加入它们

|to_entries[]|(.key+"="+.value)

关于bash - jq - 像数组一样遍历 bash 中的对象(aws 卷),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299028/

相关文章:

linux - 合并两个文本文件特定位置

json - 处理非常大的输入文件而无需slurp

jq - 为什么这个 jq 管道不需要点?

bash 在一行中打印多行单词

python - python中的多服务器监视器/自动重启器

jq - 如何用jq确定URL的最后部分?

jq:递归重命名键

json - 在 Shell 脚本中遍历 JSON 数组

bash - 如何打印当前的 bash 提示符?

bash - 如何在 vimscript 中使用 sleep 仅用于命令行栏?