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


###[jqを使ってAWSの情報を取得しよう①](https://www.hands-lab.com/tech/entry/1286.html)
コード部分だけ書き出し。Qiitaは色つくからいいね

---
###JSONの基本

JSONはキーと値のセットで表現されています。
キーと値のセットは以下の様に表されます。
また","で区切って複数のセットを列挙することができます。

```json:JSONの基本形
{"キー":"値"}
{"キー":"値","キー":"値"}
```

JSONは配列を持ち、配列は[]で表されます。
要素は","で区切って表記されます。

```JSON:サンプル①（値に配列を保つ場合）
"Tags":[
    {
        "Key": "Name",
        "Value": "test"
    }
]
```

```json:サンプル①を一列で書くと
"Tags":[{"Key":"Name","Value":"test"}]
```


```json:サンプル②（配列が複数要素を持つ場合）
"Tags": [
    {
        "Key": "hoge",
        "Value": "***********"
    },
        "Key": "fuga",
        "Value": "***********"
    }
]
```

```json:サンプル②を一列で書くと
"Tags": [ {　"Key": "hoge"　,　"Value": "*****"　} ,　{　"Key": "fuga"　,　"Value": "****"　} ]
```

---
####ｊｑでパースする

```json:describe-instances(ec2)
{
    "Reservations": [
        {
            "ReservationId": "r-********",
            "Groups": [],
            "OwnerId": "************",
            "Instances": [
                {
                    "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal",
                    "EbsOptimized": false,
                    "Monitoring": {
                        "State": "disabled"
                    },
                    "SecurityGroups": [
                        {
                            "GroupName": "RDP",
                            "GroupId": "sg-********"
                        }
                    ],
                    "State": {
                        "Name": "stopped",
                        "Code": 80
                    },
                    "NetworkInterfaces": [
                        {
                            "PrivateDnsName": "ip-********.ap-northeast-1.compute.internal",
                            "PrivateIpAddresses": [
                                {
                                    "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal",
                                    "PrivateIpAddress": "172.**.**.***",
                                    "Primary": true
                                }
                            ],
                            "OwnerId": "************",
                            "Groups": [
                                {
                                    "GroupName": "RDP",
                                    "GroupId": "sg-********"
                                }
                            ],
                            "PrivateIpAddress": "172.**.**.***",
                            "Attachment": {
                                "DeleteOnTermination": true,
                                "AttachTime": "2016-02-24T05:53:59.000Z",
                                "AttachmentId": "eni-attach-*******",
                                "Status": "attached",
                                "DeviceIndex": 0
                            },
                            "Description": "",
                            "SourceDestCheck": true,
                            "MacAddress": "************",
                            "Status": "in-use",
                            "NetworkInterfaceId": "eni-************",
                            "VpcId": "vpc-************",
                            "SubnetId": "subnet-************"
                        }
                    ],
                    "PrivateIpAddress": "172.**.**.***",
                    "RootDeviceType": "ebs",
                    "StateTransitionReason": "User initiated (2016-02-24 10:01:43 GMT)",
                    "Placement": {
                        "GroupName": "",
                        "Tenancy": "default",
                        "AvailabilityZone": "ap-northeast-1a"
                    },
                    "Hypervisor": "xen",
                    "KeyName": "macpro",
                    "RootDeviceName": "/dev/sda1",
                    "ImageId": "ami-************",
                    "LaunchTime": "2016-02-24T05:53:59.000Z",
                    "VpcId": "vpc-************",
                    "BlockDeviceMappings": [
                        {
                            "Ebs": {
                                "DeleteOnTermination": true,
                                "AttachTime": "2016-02-24T05:54:04.000Z",
                                "VolumeId": "vol-************",
                                "Status": "attached"
                            },
                            "DeviceName": "/dev/sda1"
                        }
                    ],
                    "Tags": [
                        {
                            "Key": "Name",
                            "Value": "test"
                        }
                    ],
                    "SubnetId": "subnet-************",
                    "ProductCodes": [],
                    "Architecture": "x86_64",
                    "InstanceType": "t2.micro",
                    "Platform": "windows",
                    "StateReason": {
                        "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
                        "Code": "Client.UserInitiatedShutdown"
                    },
                    "InstanceId": "i-************",
                    "SourceDestCheck": true,
                    "ClientToken": "rduVq************",
                    "AmiLaunchIndex": 0,
                    "PublicDnsName": "",
                    "VirtualizationType": "hvm"
                }
            ]
        }
    ]
}
```

####インスタンスid取得

```bash:InstanceId
aws ec2 describe-instances | jq '.Reservations[].Instances[].InstanceId'
```

```:結果
"i-************"
```

---
####セキュリティグループ一覧取得

```bash:Groups
aws ec2 describe-instances |jq '.Reservations[].Instances[].NetworkInterfaces[].Groups[]'
```

```:結果
{
  "GroupName": "RDP",
  "GroupId": "sg-984bb5fc"
}
```

GroupId取得

```bash:GroupId
aws ec2 describe-instances |jq '.Reservations[].Instances[].NetworkInterfaces[].Groups[].GroupId'
```

```:結果
"sg-984bb5fc"
```

---
####""を剥く

```bash:InstanceId
aws ec2 describe-instances | jq -r '.Reservations[].Instances[].InstanceId'
```

```:結果
i-************
```