0

I ran an aws command and the results as below:

[root@webserver-02 test]# aws ec2 describe-volumes --volume-ids vol-0be41aaca50a1d4d2 --profile donald 2> /dev/null|
jq -r '.Volumes[]|{"Tags":(.Tags | map({(.key//.Key): (.value//.Value)}) | add), VolumeId:.VolumeId,SnapshotId:.SnapshotId, Volumesize:.Size, CreateTime:.CreateTime, State:.State,"Instances": [.Attachments[0].InstanceId] }'

and the results as below:

{
  "Tags": {
    "Name": "4d2",
    "c": "c",
    "a": "a",
    "b": "b"
  },
  "VolumeId": "vol-0be41aaca50a1d4d2",
  "SnapshotId": "snap-0be43cb27ae60c978",
  "Volumesize": 8,
  "CreateTime": "2018-09-20T02:24:21.067Z",
  "State": "in-use",
  "InstanceId": "i-016c1d0448d6917f6"
}

OK. I want to display InstanceId json as follows:

{
    "Tags": {
        "Name": "4d2",
        "c": "c",
        "a": "a",
        "b": "b"
    },
    "VolumeId": "vol-0be41aaca50a1d4d2",
    "SnapshotId": "snap-0be43cb27ae60c978",
    "Volumesize": 8,
    "CreateTime": "2018-09-20T02:24:21.067Z",
    "State": "in-use",
    "Instances": [{
        "InstanceId": "i-016c1d0448d6917f6"
    }]
}

I am using jq in the bash shell.

1 Answer 1

1

You can use this jq filter:

jq '.+{"Instances":[{InstanceId}]}|del(.InstanceId)' file

This adds the object Instances to the current JSON data using the value of InstanceId.
Then it deletes the InstanceId on the object root.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.