Kubernetes 101 : An overview of JSON PATH - Advanced "kubectl" commands -



JSON PATH is a "query" language that allows us to filter and format information in environments where the amount of data is too big to be managed using the "kubectl" regular commands.

JSON PATH also allow us to get an customized output that fits our needs.

To get the right JASON PATH parameter for our "kubectl" command, we display the information received from the API-server in a JSON format like below:


Then we follow the path down our JSON file to the field we are looking for.


For example for to display the labels of all the elements in the items[] array, we follow the path to the labels as below:


The some goes for the nodes. We start by displaying our output in JSON format using the below command:


Then we navigate down our JSON file the same way we did with the pod's JSON file.

For example to get the names of the nodes, we use the below command:


Looping through an array:

We use the below command to loop through the entire items[] array:
 

The above loop fits in one line after the "- o=jsonpath=....." parameter.

Organizing our output:

For a better readability we could display our output in columns as we can see below:


As an example, we have the nodes and their CPU capacity:


Sorting our output by name - node names -:


Remark - JSON PATH syntax - :
  • $ : root of the file
  • {} : dictionary
  • [] : array
  • [*] : all the elements of the array
  • [0] : first element of the array.
  • [0,1,2] : first,second and third element of the array.

Comments

Leave as a comment:

Archive