jqチートシート

# サマリ

個人的によく使うjqコマンドの備忘録。
適宜で追加予定（多分）



# 詳細

jq使いたくなる時の状況として大体がクソデカjsonのparse用なので、サンプルも大きなものを用意。
生成方法としては適当にAWS EC2インスタンスを立てて、そのconfigをjson形式で出力した。

```json:sample.json
{
    "Reservations": [
        {
            "Groups": [],
            "Instances": [
                {
                    "AmiLaunchIndex": 0,
                    "ImageId": "ami-0595d6e81396a9efb",
                    "InstanceId": "i-0af291a70792eedcc",
                    "InstanceType": "t2.micro",
                    "KeyName": "MyKeyPair",
                    "LaunchTime": "2024-05-04T14:12:50+00:00",
                    "Monitoring": {
                        "State": "disabled"
                    },
                    "Placement": {
                        "AvailabilityZone": "ap-northeast-1c",
                        "GroupName": "",
                        "Tenancy": "default"
                    },
                    "PrivateDnsName": "ip-172-31-9-174.ap-northeast-1.compute.internal",
                    "PrivateIpAddress": "172.31.9.174",
                    "ProductCodes": [],
                    "PublicDnsName": "ec2-18-181-216-211.ap-northeast-1.compute.amazonaws.com",
                    "PublicIpAddress": "18.181.216.211",
                    "State": {
                        "Code": 16,
                        "Name": "running"
                    },
                    "StateTransitionReason": "",
                    "SubnetId": "subnet-0e107b67fa58f9d8e",
                    "VpcId": "vpc-08502774e7262a266",
                    "Architecture": "x86_64",
                    "BlockDeviceMappings": [
                        {
                            "DeviceName": "/dev/sda1",
                            "Ebs": {
                                "AttachTime": "2024-05-04T14:12:51+00:00",
                                "DeleteOnTermination": true,
                                "Status": "attached",
                                "VolumeId": "vol-0284e71624766a1a4"
                            }
                        }
                    ],
                    "ClientToken": "daf82584-2b91-4c32-b310-4a38cbfab382",
                    "EbsOptimized": false,
                    "EnaSupport": true,
                    "Hypervisor": "xen",
                    "NetworkInterfaces": [
                        {
                            "Association": {
                                "IpOwnerId": "amazon",
                                "PublicDnsName": "ec2-18-181-216-211.ap-northeast-1.compute.amazonaws.com",
                                "PublicIp": "18.181.216.211"
                            },
                            "Attachment": {
                                "AttachTime": "2024-05-04T14:12:50+00:00",
                                "AttachmentId": "eni-attach-08c3c7f2a0b6102cd",
                                "DeleteOnTermination": true,
                                "DeviceIndex": 0,
                                "Status": "attached",
                                "NetworkCardIndex": 0
                            },
                            "Description": "",
                            "Groups": [
                                {
                                    "GroupName": "default",
                                    "GroupId": "sg-0190b89b3d4a36149"
                                }
                            ],
                            "Ipv6Addresses": [],
                            "MacAddress": "0a:cf:9e:fc:4a:11",
                            "NetworkInterfaceId": "eni-0e13d5d8e11546e9d",
                            "OwnerId": "471112995230",
                            "PrivateDnsName": "ip-172-31-9-174.ap-northeast-1.compute.internal",
                            "PrivateIpAddress": "172.31.9.174",
                            "PrivateIpAddresses": [
                                {
                                    "Association": {
                                        "IpOwnerId": "amazon",
                                        "PublicDnsName": "ec2-18-181-216-211.ap-northeast-1.compute.amazonaws.com",
                                        "PublicIp": "18.181.216.211"
                                    },
                                    "Primary": true,
                                    "PrivateDnsName": "ip-172-31-9-174.ap-northeast-1.compute.internal",
                                    "PrivateIpAddress": "172.31.9.174"
                                }
                            ],
                            "SourceDestCheck": true,
                            "Status": "in-use",
                            "SubnetId": "subnet-0e107b67fa58f9d8e",
                            "VpcId": "vpc-08502774e7262a266",
                            "InterfaceType": "interface"
                        }
                    ],
                    "RootDeviceName": "/dev/sda1",
                    "RootDeviceType": "ebs",
                    "SecurityGroups": [
                        {
                            "GroupName": "default",
                            "GroupId": "sg-0190b89b3d4a36149"
                        }
                    ],
                    "SourceDestCheck": true,
                    "VirtualizationType": "hvm",
                    "CpuOptions": {
                        "CoreCount": 1,
                        "ThreadsPerCore": 1
                    },
                    "CapacityReservationSpecification": {
                        "CapacityReservationPreference": "open"
                    },
                    "HibernationOptions": {
                        "Configured": false
                    },
                    "MetadataOptions": {
                        "State": "applied",
                        "HttpTokens": "optional",
                        "HttpPutResponseHopLimit": 1,
                        "HttpEndpoint": "enabled",
                        "HttpProtocolIpv6": "disabled",
                        "InstanceMetadataTags": "disabled"
                    },
                    "EnclaveOptions": {
                        "Enabled": false
                    },
                    "BootMode": "uefi-preferred",
                    "PlatformDetails": "Linux/UNIX",
                    "UsageOperation": "RunInstances",
                    "UsageOperationUpdateTime": "2024-05-04T14:12:50+00:00",
                    "PrivateDnsNameOptions": {
                        "HostnameType": "ip-name",
                        "EnableResourceNameDnsARecord": false,
                        "EnableResourceNameDnsAAAARecord": false
                    },
                    "MaintenanceOptions": {
                        "AutoRecovery": "default"
                    },
                    "CurrentInstanceBootMode": "legacy-bios"
                }
            ],
            "OwnerId": "471112995230",
            "ReservationId": "r-05fea8fdf1007909f"
        }
    ]
}
```

## 要素確認

```bash
# トップレベルのjson propertyを確認
$  cat sample.json | jq 'keys'
[
  "Reservations"
]
```

### 直接参照する
```bash
# 配列参照
$  cat sample.json | jq  'keys[0]'
"Reservations"

# -rオプションでダブルクォーテーションが外せる
$  cat sample.json | jq -r  'keys[0]'
Reservations
```

### ネストされた要素の確認
```bash
$  cat sample.json | jq  '.Reservations | keys'
[
  0,
  1
]

# 配列参照
$  cat sample.json | jq  '.Reservations[0] | keys'
[
  "Groups",
  "Instances",
  "OwnerId",
  "ReservationId"
]
```

### 更にネストされた要素の確認
```bash
# 同様の記述で参照が出来る
$  cat sample.json | jq  '.Reservations[0].Instances | keys'
[
  0
]

# 配列参照
$  cat sample.json | jq  '.Reservations[0].Instances[0] | keys'
[
  "AmiLaunchIndex",
  "Architecture",
  "BlockDeviceMappings",
  "BootMode",
  "CapacityReservationSpecification",
  "ClientToken",
  "CpuOptions",
  "CurrentInstanceBootMode",
  "EbsOptimized",
  "EnaSupport",
  "EnclaveOptions",
  "HibernationOptions",
  "Hypervisor",
  "ImageId",
  "InstanceId",
  "InstanceType",
  "KeyName",
  "LaunchTime",
  "MaintenanceOptions",
  "MetadataOptions",
  "Monitoring",
  "NetworkInterfaces",
  "Placement",
  "PlatformDetails",
  "PrivateDnsName",
  "PrivateDnsNameOptions",
  "PrivateIpAddress",
  "ProductCodes",
  "PublicDnsName",
  "PublicIpAddress",
  "RootDeviceName",
  "RootDeviceType",
  "SecurityGroups",
  "SourceDestCheck",
  "State",
  "StateTransitionReason",
  "SubnetId",
  "UsageOperation",
  "UsageOperationUpdateTime",
  "VirtualizationType",
  "VpcId"
]
```

## 特定要素の値を取得する

Reservations.Instancesのプロパティ参照する

### 単一取得
```bash
# 直接プロパティ名を指定する。"."を接頭するのを忘れないように
$  cat sample.json | jq  '.Reservations[0].Instances[0] | .VpcId'
"vpc-08502774e7262a266"
```

### 複数取得
```bash
# 複数出力の場合、出力形式をkey-value型にしないとerrorになる
$ cat sample.json | jq  '.Reservations[0].Instances[0] | {VpcId: .VpcId, VirtualizationType: .VirtualizationType}'
{
  "VpcId": "vpc-08502774e7262a266",
  "VirtualizationType": "hvm"
}
```