I want to write a script that finds my unused volumes in AWS. (the 'status' of used volumes is 'in-use').

When I use the aws ec2 describe-volumes --filters Name=status,Values=available I got the next response for example.

{ "Volumes": [ { "Attachments": [], "AvailabilityZone": "us-east-1a", "CreateTime": "2022-04-21T08:45:29.704000+00:00", "Encrypted": false, "Size": 1, "SnapshotId": "", "State": "available", "VolumeId": "vol-0e81644ad6193439f", "Iops": 100, "Tags": [ { "Key": "sre", "Value": "test" } ], "VolumeType": "gp2", "MultiAttachEnabled": false } ] }

I want to save just the state, volumeId & tags.

  • It's unclear what you want the result to look like given the JSON document that you show. Do you still want a JSON document, but with some information removed, or do you want the data in some other format? What should that output look like if the arrays in the JSON document contains more than single elements (the Volumes array and the Tags array)?
    – Kusalananda
    Commented Apr 21, 2022 at 19:53
  • I want the result looks like { "State": "available","VolumeId": "vol-0e81644ad6193439f","Tags": [ { "Key": "sre", "Value": "test" } ]
    – AdiIsrael
    Commented Apr 21, 2022 at 19:57
  • They can be in a list.
    – AdiIsrael
    Commented Apr 21, 2022 at 19:59

1 Answer 1


Assuming you can't use the aws command to do this, I would personally use the JSON parser jq to extract the wanted data.

jq '.Volumes[] | { State: .State, VolumeId: .VolumeId, Tags: .Tags }'

For each entry in the Volumes array, this would create a new JSON object containing only the fields you want to keep from that entry.

With your given JSON document as input, this would result in

  "State": "available",
  "VolumeId": "vol-0e81644ad6193439f",
  "Tags": [
      "Key": "sre",
      "Value": "test"

Or, if you use jq -c:


... which is an equivalent document.

  • When I using my script I have 'awsvalue' which contain the dictionary from aws response, so let say I want print like you mention up . How I combine between jq '.Volumes[] | { State: .State, VolumeId: .VolumeId, Tags: .Tags }' & 'awsvalue' ?
    – AdiIsrael
    Commented Apr 21, 2022 at 20:19
  • awsvalue=$(aws ec2 describe-volumes --filters Name=status,Values=available) ###echo ${awsvalue[jq '.Volumes[] | { State: .State, VolumeId: .VolumeId, Tags: .Tags }'] ?
    – AdiIsrael
    Commented Apr 21, 2022 at 20:22
  • @AdiIsrael aws ec2 ... | jq '...'. There is no need to store the JSON document in an intermediate variable.
    – Kusalananda
    Commented Apr 21, 2022 at 20:39
  • Thanks!!! it helped and I am learning jq , thanks a lot.
    – AdiIsrael
    Commented Apr 21, 2022 at 20:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .