Skip to main content
Hello, and welcome to this lesson on using JSONPath with kubectl in Kubernetes. My name is Mumshad Mannambeth. In this session, we will explain why JSONPath is an essential tool for managing large Kubernetes outputs and demonstrate how to use it effectively with kubectl. We’ll cover viewing JSON output, constructing JSONPath queries, using loops, custom column formatting, and sorting functions. Towards the end, you’ll find practice tests that will help reinforce your learning.
The image lists objectives for learning JSON PATH in KubeCtl, including usage, examples, loops, custom columns, sorting, and practice tests.
To kick off, consider some of these basic commands:
Before diving deeper, it’s important to understand how JSONPath queries work. If you’re new to JSONPath, consider exploring free online tutorials and practice tests to build a strong foundation.
The image lists prerequisites for JSON PATH, including YouTube tutorials and KodeKloud practice tests, with a link to a JSON PATH quiz on KodeKloud.
Experiment with JSONPath on a Kubernetes dataset to understand its structure before applying more complex queries with kubectl.

Why Use JSONPath?

In production environments with hundreds of nodes and thousands of objects—such as deployments, pods, ReplicaSets, services, and secrets—the standard kubectl output might not provide the precise data you need. While kubectl gives a quick summary, JSONPath queries let you filter and format large datasets with precision. This functionality enables you to generate customized reports tailored to your needs.
The image is a slide titled "Why JSON Path?" discussing large datasets, including hundreds of nodes and thousands of PODs, Deployments, and ReplicaSets.

How Kubectl Works

Kubectl is the command-line interface for Kubernetes that communicates with the API server. Every command retrieves data in JSON format and converts it into a human-readable format. For example, the basic command to list nodes is:
The typical output looks like this:
Behind the scenes, the JSON output might resemble:
Kubectl trims excessive details to keep the output clear. For a more detailed view, use the -o wide option:
This might return:
If you need the full details—like resource capacity, taints, conditions, hardware architecture, etc.—use kubectl describe or query the JSON directly.

Building a JSONPath Query

Imagine needing a report that displays specific fields, such as node names and CPU counts. No default command provides this directly—that’s where JSONPath shines. Follow these steps to build a JSONPath query with kubectl:
  1. Retrieve Raw Data
    Start by retrieving the node details:
  2. View Full JSON Output
    Add the -o json option to see the complete data:
    For instance, a sample JSON output for pods might look like:
  3. Analyze the JSON Structure
    For example, to extract the image from the first container in the first pod, the JSONPath query is:
    • Query: .items[0].spec.containers[0].image
  4. Query with Kubectl
    Embed your query in single quotes and curly braces:
If you’re new to JSONPath, try using online evaluators like jsonpath.com to experiment with and refine your queries.
Using JSONPath, you can extract various fields. For example:
  • {.items[*].metadata.name} returns all node names.
  • {.items[*].status.nodeInfo.architecture} provides hardware architectures.
  • {.items[*].status.capacity.cpu} shows CPU counts per node.
Example command:
Output:
Similarly:
Output:
And:
Output:
You can even combine queries. For instance, to display node names on one line and their CPU counts on the next:
This produces:

Advanced Formatting with Loops and Custom Columns

For a neatly formatted table, you can leverage JSONPath loops. Using the range keyword functions as an iterator over each node. For every node, you can print its name, followed by a tab (\t), its CPU count, and a newline (\n):
Alternatively, the built-in custom columns functionality provides a simpler option. For example:
Note that with custom columns, you don’t need to include the .items in your JSONPath query because kubectl automatically iterates through the list. If you need to sort the output, use the --sort-by option. For example:
The image shows Kubernetes JSON path queries displaying node details like CPU, taints, architecture, and images associated with names like nginx, ubuntu, and redis.

Final Thoughts

Mastering JSONPath queries with kubectl empowers you to extract and present detailed information from your Kubernetes environment accurately and efficiently. With practice, you’ll create powerful commands that display data in exactly the format you need. Below is a summary of key commands demonstrated in this lesson: Now, explore the practice tests and exercises to further solidify your understanding of these advanced kubectl commands. Happy learning!

Watch Video

Practice Lab