[AWS]aws-cliでJSONを扱うためにーｊｑ基本編②ー

[[AWS]aws-cliでJSONを扱うためにーｊｑ基本編ー①](http://qiita.com/Yuki_BB3/items/d4af0f8301790be2a309)のつづき。
前回同様ブログの記事を見やすいのでこっちにも書いている感じ。
詳細はブログの方に書いてます。
[jqを使ってAWSの情報を取得しよう②｜ハンズラボエンジニアブログ](https://www.hands-lab.com/tech/entry/1328.html)

今回使用するサンプルのJSON
インスタンス２台分の情報を出力しています。

```json:smaple-json
{
    "Reservations": [
        {
            "OwnerId": "************", 
            "ReservationId": "r-********", 
            "Groups": [], 
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    }, 
                    "PublicDnsName": "ec2-*************.ap-northeast-1.compute.amazonaws.com", 
                    "State": {
                        "Code": 16, 
                        "Name": "running"
                    }, 
                    "EbsOptimized": false, 
                    "LaunchTime": "2016-03-08T08:25:51.000Z", 
                    "PublicIpAddress": "************", 
                    "PrivateIpAddress": "172.31.23.149", 
                    "ProductCodes": [], 
                    "VpcId": "vpc-********", 
                    "StateTransitionReason": "", 
                    "InstanceId": "i-********", 
                    "ImageId": "ami-59bdb937", 
                    "PrivateDnsName": "ip-172-31-23-149.ap-northeast-1.compute.internal", 
                    "KeyName": "ec2kye", 
                    "SecurityGroups": [
                        {
                            "GroupName": "launch-wizard-4", 
                            "GroupId": "sg-********"
                        }
                    ], 
                    "ClientToken": "AKWfs123456789", 
                    "SubnetId": "subnet-********", 
                    "InstanceType": "t2.micro", 
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use", 
                            "MacAddress": "06:e4:22:c1:df:c5", 
                            "SourceDestCheck": true, 
                            "VpcId": "vpc-********", 
                            "Description": "", 
                            "Association": {
                                "PublicIp": "*************", 
                                "PublicDnsName": "ec2-*********.ap-northeast-1.compute.amazonaws.com", 
                                "IpOwnerId": "amazon"
                            }, 
                            "NetworkInterfaceId": "eni-********", 
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "ip-172-31-23-149.ap-northeast-1.compute.internal", 
                                    "Association": {
                                        "PublicIp": "************", 
                                        "PublicDnsName": "ec2-***********.ap-northeast-1.compute.amazonaws.com", 
                                        "IpOwnerId": "amazon"
                                    }, 
                                    "Primary": true, 
                                    "PrivateIpAddress": "172.31.23.149"
                                }
                            ], 
                            "PrivateDnsName": "ip-172-31-23-149.ap-northeast-1.compute.internal", 
                            "Attachment": {
                                "Status": "attached", 
                                "DeviceIndex": 0, 
                                "DeleteOnTermination": true, 
                                "AttachmentId": "eni-attach-*********", 
                                "AttachTime": "2016-03-08T08:25:51.000Z"
                            }, 
                            "Groups": [
                                {
                                    "GroupName": "launch-wizard-1", 
                                    "GroupId": "sg-*********"
                                }
                            ], 
                            "SubnetId": "subnet-*************", 
                            "OwnerId": "************", 
                            "PrivateIpAddress": "172.31.23.149"
                        }
                    ], 
                    "SourceDestCheck": true, 
                    "Placement": {
                        "Tenancy": "default", 
                        "GroupName": "", 
                        "AvailabilityZone": "ap-northeast-1a"
                    }, 
                    "Hypervisor": "xen", 
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/xvda", 
                            "Ebs": {
                                "Status": "attached", 
                                "DeleteOnTermination": true, 
                                "VolumeId": "vol-**********", 
                                "AttachTime": "2016-03-08T08:25:52.000Z"
                            }
                        }
                    ], 
                    "Architecture": "x86_64", 
                    "RootDeviceType": "ebs", 
                    "RootDeviceName": "/dev/xvda", 
                    "VirtualizationType": "hvm", 
                    "AmiLaunchIndex": 0
                }
            ]
        }, 
        {
            "OwnerId": "**************", 
            "ReservationId": "r-**********", 
            "Groups": [], 
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    }, 
                    "PublicDnsName": "", 
                    "Platform": "windows", 
                    "State": {
                        "Code": 80, 
                        "Name": "stopped"
                    }, 
                    "EbsOptimized": false, 
                    "LaunchTime": "2016-02-24T05:53:59.000Z", 
                    "PrivateIpAddress": "172.31.31.197", 
                    "ProductCodes": [], 
                    "VpcId": "vpc-c**********", 
                    "StateTransitionReason": "User initiated (2016-02-24 10:01:43 GMT)", 
                    "InstanceId": "i-**********", 
                    "ImageId": "ami-2044434e", 
                    "PrivateDnsName": "ip-172-31-31-197.ap-northeast-1.compute.internal", 
                    "KeyName": "macpro", 
                    "SecurityGroups": [
                        {
                            "GroupName": "RDP", 
                            "GroupId": "sg-**********"
                        }
                    ], 
                    "ClientToken": "rduVq123456789", 
                    "SubnetId": "subnet-*********", 
                    "InstanceType": "t2.micro", 
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use", 
                            "MacAddress": "06:34:e3:b1:a2:0d", 
                            "SourceDestCheck": true, 
                            "VpcId": "vpc-********", 
                            "Description": "", 
                            "NetworkInterfaceId": "eni-**********", 
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "ip-172-31-31-197.ap-northeast-1.compute.internal", 
                                    "Primary": true, 
                                    "PrivateIpAddress": "172.31.31.197"
                                }
                            ], 
                            "PrivateDnsName": "ip-172-31-31-197.ap-northeast-1.compute.internal", 
                            "Attachment": {
                                "Status": "attached", 
                                "DeviceIndex": 0, 
                                "DeleteOnTermination": true, 
                                "AttachmentId": "eni-attach-********", 
                                "AttachTime": "2016-02-24T05:53:59.000Z"
                            }, 
                            "Groups": [
                                {
                                    "GroupName": "RDP", 
                                    "GroupId": "sg-**********"
                                }
                            ], 
                            "SubnetId": "subnet-*********", 
                            "OwnerId": "************", 
                            "PrivateIpAddress": "172.31.31.197"
                        }
                    ], 
                    "SourceDestCheck": true, 
                    "Placement": {
                        "Tenancy": "default", 
                        "GroupName": "", 
                        "AvailabilityZone": "ap-northeast-1a"
                    }, 
                    "Hypervisor": "xen", 
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/sda1", 
                            "Ebs": {
                                "Status": "attached", 
                                "DeleteOnTermination": true, 
                                "VolumeId": "vol-**********", 
                                "AttachTime": "2016-02-24T05:54:04.000Z"
                            }
                        }
                    ], 
                    "Architecture": "x86_64", 
                    "StateReason": {
                        "Message": "Client.UserInitiatedShutdown: User initiated shutdown", 
                        "Code": "Client.UserInitiatedShutdown"
                    }, 
                    "RootDeviceName": "/dev/sda1", 
                    "VirtualizationType": "hvm", 
                    "RootDeviceType": "ebs", 
                    "Tags": [
                        {
                            "Value": "test", 
                            "Key": "Name"
                        }
                    ], 
                    "AmiLaunchIndex": 0
                }
            ]
        }
    ]
}

```

では今回もさくさくやっていきます。

##select()
条件に一致する要素のみを抽出する。

```jq:起動中のインスタンスのみ抽出
aws ec2 describe-instances | jq '.Reservations[] | select(.Instances[].State.Name == "running")'
```


```:結果

{
            "OwnerId": "************", 
            "ReservationId": "r-********", 
            "Groups": [], 
            "Instances": [
                {
                    "Monitoring": {
                        "State": "disabled"
                    }, 
                    "PublicDnsName": "ec2-*************.ap-northeast-1.compute.amazonaws.com", 
                    "State": {
                        "Code": 16, 
                        "Name": "running"
                    }, 
                    "EbsOptimized": false, 
                    "LaunchTime": "2016-03-08T08:25:51.000Z", 
                    "PublicIpAddress": "************", 
                    "PrivateIpAddress": "172.31.23.149", 
                    "ProductCodes": [], 
                    "VpcId": "vpc-********", 
                    "StateTransitionReason": "", 
                    "InstanceId": "i-********", 
                    "ImageId": "ami-59bdb937", 
                    "PrivateDnsName": "ip-172-31-23-149.ap-northeast-1.compute.internal", 
                    "KeyName": "ec2kye", 
                    "SecurityGroups": [
                        {
                            "GroupName": "launch-wizard-4", 
                            "GroupId": "sg-********"
                        }
                    ], 
                    "ClientToken": "AKWfs123456789", 
                    "SubnetId": "subnet-********", 
                    "InstanceType": "t2.micro", 
                    "NetworkInterfaces": [
                        {
                            "Status": "in-use", 
                            "MacAddress": "06:e4:22:c1:df:c5", 
                            "SourceDestCheck": true, 
                            "VpcId": "vpc-********", 
                            "Description": "", 
                            "Association": {
                                "PublicIp": "*************", 
                                "PublicDnsName": "ec2-*********.ap-northeast-1.compute.amazonaws.com", 
                                "IpOwnerId": "amazon"
                            }, 
                            "NetworkInterfaceId": "eni-********", 
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "ip-172-31-23-149.ap-northeast-1.compute.internal", 
                                    "Association": {
                                        "PublicIp": "************", 
                                        "PublicDnsName": "ec2-***********.ap-northeast-1.compute.amazonaws.com", 
                                        "IpOwnerId": "amazon"
                                    }, 
                                    "Primary": true, 
                                    "PrivateIpAddress": "172.31.23.149"
                                }
                            ], 
                            "PrivateDnsName": "ip-172-31-23-149.ap-northeast-1.compute.internal", 
                            "Attachment": {
                                "Status": "attached", 
                                "DeviceIndex": 0, 
                                "DeleteOnTermination": true, 
                                "AttachmentId": "eni-attach-*********", 
                                "AttachTime": "2016-03-08T08:25:51.000Z"
                            }, 
                            "Groups": [
                                {
                                    "GroupName": "launch-wizard-1", 
                                    "GroupId": "sg-*********"
                                }
                            ], 
                            "SubnetId": "subnet-*************", 
                            "OwnerId": "************", 
                            "PrivateIpAddress": "172.31.23.149"
                        }
                    ], 
                    "SourceDestCheck": true, 
                    "Placement": {
                        "Tenancy": "default", 
                        "GroupName": "", 
                        "AvailabilityZone": "ap-northeast-1a"
                    }, 
                    "Hypervisor": "xen", 
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/xvda", 
                            "Ebs": {
                                "Status": "attached", 
                                "DeleteOnTermination": true, 
                                "VolumeId": "vol-**********", 
                                "AttachTime": "2016-03-08T08:25:52.000Z"
                            }
                        }
                    ], 
                    "Architecture": "x86_64", 
                    "RootDeviceType": "ebs", 
                    "RootDeviceName": "/dev/xvda", 
                    "VirtualizationType": "hvm", 
                    "AmiLaunchIndex": 0
                }
            ]
        }, 
```

##出力を再度jqに喰わせる
パイプでつなぎます

```bash:上記コマンドを再度ｊｑに入力

aws ec2 describe-instances | jq '.Reservations[] | select(.Instances[].State.Name == "stopped")' | jq '.Instances[].NetworkInterfaces[].VpcId'

```


```:結果
"vpc-********"
```

##JSONの再整形
jqでJSONを書くことが出来ます。
出力された結果を再度JSONの形式にします。

```bash:JSONの再整形
aws ec2 describe-instances | jq '.Reservations[] | select(.Instances[].State.Name == "stopped") | {"instance id":.Instances[].InstanceId,"instance parm":[{"VPC ID":.Instances[].NetworkInterfaces[].VpcId}],"instance type":.Instances[].InstanceType}'
```

```:結果
{
  "instance id": "i-********",
  "instance parm": [
    {
      "VPC ID": "vpc-c06de9a5"
    }
  ],
  "instance type": "vpc-********"
}
```

こんな具合にJSON出力しておくと、次の処理に喰わせる事ができます。