Going through thousands of these resources to find such exact information would be a daunting task. As a result, kubectl includes the Jsonpath option, which makes filtering data across big data sets a breeze.
The kubectl command interacts with the Kubernetes API every time you run it. The Kube-apiserver then sends a JSON-formatted response. kubectl translates it to a human-readable format and displays it on the screen. To make an output understandable, a lot of information from the answer is buried during this procedure, leaving only the relevant fields visible. We can use the -o wide option to get more of it, but this isn’t all of it. There are many more details that aren’t presented.
You will need to install Ubuntu 20.04 on your Linux operating system to run the instructions in Kubernetes. Additionally, you must install the Minikube cluster on your machine in order to run Kubernetes on Linux. Minikube provides an extraordinarily seamless experience by allowing you to test commands and applications in a methodical manner. As a result, it provides the greatest learning environment for Kubernetes newbies.
The first step is to begin the Minikube cluster. Then, navigate to the command line terminal in Ubuntu 20.04, which you have installed. You can do so by pressing the Ctrl+Alt+T shortcut key or typing “Terminal” into the Ubuntu 20.04 system’s search box. Either of the aforementioned techniques will launch the entire terminal. The Minikube will thereafter be started. Enter the command “minikube start” in the terminal to start the Minikube. The Kubernetes cluster will be started, and a virtual machine capable of running a single node cluster will be created. It will also work on the kubectl installation. This too will interact with the cluster.
How to Split the Output of Kubectl Jsonpath into Separate Lines?
All programmers must Google how to get kubectl to output JSON path results on distinct lines at all times. The following command, for example, retrieves the podIP for every running Pod in all namespaces. It gives you something like this as a result. That is unquestionably not the most user-friendly result.
You can iterate over the list with the Jsonpath range function, adding a new line after each member with n. Awesome! We may now use all of the normal UNIX tools that act on new lines to work with the output (e.g. sort, xargs, uniq, etc.).
Other whitespace characters can also be used. Assume we wanted to print the Pod namespaces/names, as well as their IP addresses, separated by a comma. It’s sometimes useful to output in Jsonpath.
Example of Kubectl and Jsonpath Expressions
Jsonpath template is supported by Kubectl. Jsonpath expressions are encased in curly braces in the Jsonpath template. Kubectl employs Jsonpath expressions to successfully format the result. Also, filters the precise options in the JSON object. The syntax (mentioned below) is valid in addition to the inventive Jsonpath template syntax. You can also consider the below-mentioned example to understand all the elements.
- To quote plain text inside Jsonpath expressions, insert double quotes.
- Iterate lists utilizing the range and end operators.
- Negative slice catalogues are used to go backward through a list.
- @ is the current object
- [ ] or . is the child operator
- . . recursive descent
- * is used to get all objects
- [,]is the union operator
- ” is used to quote interpreted string
Because the expression always begins from the root object by default, the $ operator is not required. The String() function is used to print the result object.
The complete JSON input is shown in the following screenshots.
In the following code, kubectl and Jsonpath expressions are used to display the output.
Any Jsonpath template that uses spaces must be enclosed in double-quoted on Windows. It should not be written in a single quote as shown above for bash. As a result, all literals in the template must be surrounded by a single quote or an escaped double quote. Consider the following piece of code.
Note: For Jsonpath output, kubectl does not accept regular expressions. You can use a program like jq if you wish to match using regular expressions.
The following code prints the name and start time.
This guide was all about Kubernetes JSON and why are they used. We have also gone through detailed examples of how Kubectl and Jsonpath expressions work to display the specific output.