# jq

### Install

{% code overflow="wrap" %}

```bash
brew install jq
```

{% endcode %}

### Example output

```bash
aws ec2 describe-security-groups

{
    "SecurityGroups": [
        {
            "Description": "default VPC security group",
            "GroupName": "default",
            "IpPermissions": [
                {
                    "IpProtocol": "-1",
                    "IpRanges": [],
                    "Ipv6Ranges": [],
                    "PrefixListIds": [],
                    "UserIdGroupPairs": [
                        {
                            "GroupId": "sg-0494280510832e7b2",
[snip]  
```

### View All Elements for a Key

* Returns all the top keys under `SecurityGroups`

{% code overflow="wrap" %}

```bash
aws ec2 describe-security-groups | jq -r '.SecurityGroups[] | keys_unsorted[]'

Description
GroupName
IpPermissions
OwnerId
GroupId
IpPermissionsEgress
VpcId
```

{% endcode %}

* If, in this case, there are multiple Security Groups returned the above would return the same Keys multiple times, one set per security group.
* We can alter the query to only return the Keys for one set of data by adding a `0`&#x20;

{% code overflow="wrap" %}

```bash
aws ec2 describe-security-groups | jq -r '.SecurityGroups[0] | keys_unsorted[]'
```

{% endcode %}

### View All Elements for a Sub-Key

* Returns all the top sub-keys under `SecurityGroups.IpPermissions`

{% code overflow="wrap" %}

```bash
aws ec2 describe-security-groups | jq -r '.SecurityGroups[].IpPermissions[] | keys_unsorted[]'

IpProtocol
IpRanges
Ipv6Ranges
PrefixListIds
UserIdGroupPairs
```

{% endcode %}

### Convert Output into Colorized JSON

* Makes the returned output pretty JSON and in color.

```bash
ec2 describe-security-groups | jq
```

### View Certain Keys

* Only return data for a specific key.

```bash
aws ec2 describe-security-groups | jq -r '.SecurityGroups[].Description'

default VPC security group
testing jq
```

### Key is Exactly

* Only return data matching the condition, in this case, where `GroupId` equals `"sg-0021f1e76215c0548"`

{% code overflow="wrap" %}

```bash
aws ec2 describe-security-groups | jq '.SecurityGroups[] | select(.GroupId == "sg-0021f1e76215c0548")'

{
  "Description": "testing jq",
  "GroupName": "testing",
  "IpPermissions": [],
  "OwnerId": "024318953427",
  "GroupId": "sg-0021f1e76215c0548",
  "IpPermissionsEgress": [
    {
      "IpProtocol": "-1",
      "IpRanges": [
        {
          "CidrIp": "0.0.0.0/0"
        }
      ],
      "Ipv6Ranges": [],
      "PrefixListIds": [],
      "UserIdGroupPairs": []
    }
  ],
  "VpcId": "vpc-0f69e3f015d5c2b7a"
}
```

{% endcode %}

### Key Contains

* Only return data matching the condition, in this case, where `IpProtocol` contains `-1`

{% code overflow="wrap" %}

```bash
aws ec2 describe-security-groups | jq -r '.SecurityGroups[].IpPermissionsEgress[] | select(.IpProtocol | contains("-1"))'

{
  "IpProtocol": "-1",
  "IpRanges": [
    {
      "CidrIp": "0.0.0.0/0"
    }
  ],
  "Ipv6Ranges": [],
  "PrefixListIds": [],
  "UserIdGroupPairs": []
}
{
  "IpProtocol": "-1",
  "IpRanges": [
    {
      "CidrIp": "0.0.0.0/0"
    }
  ],
  "Ipv6Ranges": [],
  "PrefixListIds": [],
  "UserIdGroupPairs": []
}
```

{% endcode %}

### Return Keys After Filtering

* Return certain keys for the data matching the condition, in this case, return the `GroupName` and `GroupId` for all Security Groups containing a rule allowing all protocols, `-1`
* Also, customize the formatting for the output.

{% code overflow="wrap" %}

```bash
aws ec2 describe-security-groups | jq -r '.SecurityGroups[] | select(.IpPermissionsEgress[].IpProtocol | contains("-1")) | "\nGroup Name: \(.GroupName)\nGroup Id: \(.GroupId)"'

Group Name: default
Group Id: sg-0494280510832e7b2

Group Name: testing
Group Id: sg-0021f1e76215c0548
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.techwithtyler.dev/coding-and-cli-tooling/cli-tools/jq.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
