Skip to content

Instantly share code, notes, and snippets.

@mgodf89
Last active March 3, 2024 15:54
Show Gist options
  • Save mgodf89/86929499af39ad1ddf7a597f8dabf5cf to your computer and use it in GitHub Desktop.
Save mgodf89/86929499af39ad1ddf7a597f8dabf5cf to your computer and use it in GitHub Desktop.

jq Cheatsheet

This is as much as I needed to know for the Certified Kubernetes Administrator (CKA) exam, no more, no less.

Sections

  • A. Skill List
  • B. Example Query
  • C. Output/Result
  • D. Appendix: data.json

A. Skill List

  • List all keys

  • sort_by and reverse for sort asc/desc

  • group_by to group by a filter condition

  • map to specify fields

  • How to get things from a list

    • k get node -o json | jq ...
    • k get pod -o json | jq ...
    • k get deploy -o json | jq ...
  • How to select from a list based on a sub-element

    • k get pod -o json | jq '.items[] | select( .metadata.labels.LABEL1 == "LABEL1_TEXT")
  • Also Useful: tail -n +2 to strip first line

    • ps aux | head | tail -n +2

B. Example Query

# What to test
CONDITION1='.name == "list_item8"'
CONDITION2='.name == "list_item10"'
FULL_CONDITION=${CONDITION1}' or '${CONDITION2}

# Path to item to test
CONDITION_PATH='.feat.opt.detail.detail_list[]'

# Assembled Query for jq
QUERY='.items[] | select( '${CONDITION_PATH}' | '${FULL_CONDITION}')'

# Print Output
jq "[${QUERY}]" data.json

echo ""
echo "Completed Query:"
echo ${QUERY}
echo ""
echo "Top-Level Keys:"
jq "keys_unsorted" data.json

# Rename/Select Fields w/ Map, Aggregate List, Sortby Desc, Groupby Even/Odd
CONDITION_PATH=".items[] | .feat.opt.detail.detail_list[]"
SORTBY_PAYLOAD_DESC="sort_by(.PAYLOAD) | reverse"
FIELD_MAP="map({\"PAYLOAD\":.payload,\"NAME\":.name})"
GROUPBY_PAYLOAD_EVENODD="group_by(.PAYLOAD % 2)"

echo "Rename/Select Fields w/ Map, Aggregate List, Sortby Desc, Groupby Even/Odd:"
jq "[${CONDITION_PATH}] | ${FIELD_MAP} | ${SORTBY_PAYLOAD_DESC} | ${GROUPBY_PAYLOAD_EVENODD} | .[]" data.json

C. Output/Result

[
  {
    "name": "third_item",
    "feat": {
      "feat_name": "third_feat",
      "feat_value": "c",
      "opt": {
        "opt_name": "third_opt",
        "opt_value": "DOOD",
        "detail": {
          "detail_name": "third_detail",
          "detail_value": 789,
          "detail_list": [
            {
              "name": "list_item7",
              "payload": 7
            },
            {
              "name": "list_item8",
              "payload": 8
            },
            {
              "name": "list_item9",
              "payload": 9
            }
          ]
        }
      }
    }
  },
  {
    "name": "fourth_item",
    "feat": {
      "feat_name": "fourth_feat",
      "feat_value": "d",
      "opt": {
        "opt_name": "fourth_opt",
        "opt_value": "EOOE",
        "detail": {
          "detail_name": "fourth_detail",
          "detail_value": 101112,
          "detail_list": [
            {
              "name": "list_item10",
              "payload": 10
            },
            {
              "name": "list_item11",
              "payload": 11
            },
            {
              "name": "list_item12",
              "payload": 12
            }
          ]
        }
      }
    }
  }
]

Completed Query:
.items[] | select( .feat.opt.detail.detail_list[] | .name == "list_item8" or .name == "list_item10")

Top-Level Keys:
[
  "metadata",
  "items"
]

Rename/Select Fields w/ Map, Aggregate List, Sortby Desc, Groupby Even/Odd:
[
  {
    "PAYLOAD": 12,
    "NAME": "list_item12"
  },
  {
    "PAYLOAD": 10,
    "NAME": "list_item10"
  },
  {
    "PAYLOAD": 8,
    "NAME": "list_item8"
  },
  {
    "PAYLOAD": 6,
    "NAME": "list_item6"
  },
  {
    "PAYLOAD": 4,
    "NAME": "list_item4"
  },
  {
    "PAYLOAD": 2,
    "NAME": "list_item2"
  }
]
[
  {
    "PAYLOAD": 11,
    "NAME": "list_item11"
  },
  {
    "PAYLOAD": 9,
    "NAME": "list_item9"
  },
  {
    "PAYLOAD": 7,
    "NAME": "list_item7"
  },
  {
    "PAYLOAD": 5,
    "NAME": "list_item5"
  },
  {
    "PAYLOAD": 3,
    "NAME": "list_item3"
  },
  {
    "PAYLOAD": 1,
    "NAME": "list_item1"
  }
]

D. Appendix: data.json

{
  "metadata": "kghkjjhkyfdurxc",
  "items": [
    {
      "name": "first_item",
      "feat": {
        "feat_name": "first_feat",
        "feat_value": "a",
        "opt": {
          "opt_name": "first_opt",
          "opt_value": "BOOB",
          "detail": {
            "detail_name": "first_detail",
            "detail_value": 123
          }
        }
      }
    },
    {
      "name": "second_item",
      "feat": {
        "feat_name": "second_feat",
        "feat_value": "b",
        "opt": {
          "opt_name": "second_opt",
          "opt_value": "COOC",
          "detail": {
            "detail_name": "second_detail",
            "detail_value": 456
          }
        }
      }
    },
    {
      "name": "third_item",
      "feat": {
        "feat_name": "third_feat",
        "feat_value": "c",
        "opt": {
          "opt_name": "third_opt",
          "opt_value": "DOOD",
          "detail": {
            "detail_name": "third_detail",
            "detail_value": 789
          }
        }
      }
    },
    {
      "name": "fourth_item",
      "feat": {
        "feat_name": "fourth_feat",
        "feat_value": "d",
        "opt": {
          "opt_name": "fourth_opt",
          "opt_value": "EOOE",
          "detail": {
            "detail_name": "fourth_detail",
            "detail_value": 101112
          }
        }
      }
    }
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment