title: jq - Iterate over objects in bash like an array (aws volumes)
body: <p>I have some JSON that I have pulled from AWS and formatted with <strong>jq</strong> (the original code is at the bottom) to give me the following output:</p>
  
  <pre><code>{
    "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"
    }
  }
  </code></pre>
  
  <p>What I now need to do is to pull the 'VolumeId', 'Period', 'Start' and 'Finish'. I would like to iterate over these objects put these into 4 bash variables of the same name in a <code>for</code> loop.</p>
  
  <p>e.g.</p>
  
  <pre><code>VolumeId="vol-33333333"
  Period="1"
  Start="00:00"
  Finish="00:20"
  </code></pre>
  
  <p>The problem is that if I put the entire JSON into a variable, it is treated as a single argument. I could use something like <code>mapfile</code>, however it would then turn it into too many arguments - e.g.</p>
  
  <pre><code>}
  "Volumes": [
  { 
  </code></pre>
  
  <p>etc</p>
  
  <p>Any help in getting this to work would be greatly appreciated. The end result is to be able to take a snapshot of the volume and use the 'Period' tag to calculate retention etc.</p>
  
  <p>-- 
  Original JSON:</p>
  
  <pre><code>{
  "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"
      }
  ]
  }
  </code></pre>
  
  <p>and the <strong>jq</strong> command:</p>
  
  <pre><code>jq -r '.Volumes[] | {"VolumeId": .VolumeId, "Tags": [.Tags[]] | from_entries}' 
  </code></pre>