JSONPath Support

Kubectl supports JSONPath template.

JSONPath template is composed of JSONPath expressions enclosed by curly braces {}. Kubectl uses JSONPath expressions to filter on specific fields in the JSON object and format the output. In addition to the original JSONPath template syntax, the following functions and syntax are valid:

  1. Use double quotes to quote text inside JSONPath expressions.
  2. Use the range, end operators to iterate lists.
  3. Use negative slice indices to step backwards through a list. Negative indices do not “wrap around” a list and are valid as long as -index + listLength >= 0.

Note:

  • The $ operator is optional since the expression always starts from the root object by default.

  • The result object is printed as its String() function.

Given the JSON input:

  1. {
  2. "kind": "List",
  3. "items":[
  4. {
  5. "kind":"None",
  6. "metadata":{"name":"127.0.0.1"},
  7. "status":{
  8. "capacity":{"cpu":"4"},
  9. "addresses":[{"type": "LegacyHostIP", "address":"127.0.0.1"}]
  10. }
  11. },
  12. {
  13. "kind":"None",
  14. "metadata":{"name":"127.0.0.2"},
  15. "status":{
  16. "capacity":{"cpu":"8"},
  17. "addresses":[
  18. {"type": "LegacyHostIP", "address":"127.0.0.2"},
  19. {"type": "another", "address":"127.0.0.3"}
  20. ]
  21. }
  22. }
  23. ],
  24. "users":[
  25. {
  26. "name": "myself",
  27. "user": {}
  28. },
  29. {
  30. "name": "e2e",
  31. "user": {"username": "admin", "password": "secret"}
  32. }
  33. ]
  34. }
FunctionDescriptionExampleResult
textthe plain textkind is {.kind}kind is List
@the current object{@}the same as input
. or []child operator{.kind}, {[‘kind’]} or {[‘name.type’]}List
..recursive descent{..name}127.0.0.1 127.0.0.2 myself e2e
wildcard. Get all objects{.items[].metadata.name}[127.0.0.1 127.0.0.2]
[start:end:step]subscript operator{.users[0].name}myself
[,]union operator{.items[][‘metadata.name’, ‘status.capacity’]}127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8]
?()filter{.users[?(@.name==”e2e”)].user.password}secret
range, enditerate list{range .items[]}[{.metadata.name}, {.status.capacity}] {end}[127.0.0.1, map[cpu:4]] [127.0.0.2, map[cpu:8]]
‘’quote interpreted string{range .items[*]}{.metadata.name}{‘\t’}{end}127.0.0.1 127.0.0.2

Examples using kubectl and JSONPath expressions:

  1. kubectl get pods -o json
  2. kubectl get pods -o=jsonpath='{@}'
  3. kubectl get pods -o=jsonpath='{.items[0]}'
  4. kubectl get pods -o=jsonpath='{.items[0].metadata.name}'
  5. kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'status.capacity']}"
  6. kubectl get pods -o=jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.status.startTime}{"\n"}{end}'

Note:

On Windows, you must double quote any JSONPath template that contains spaces (not single quote as shown above for bash). This in turn means that you must use a single quote or escaped double quote around any literals in the template. For example:

  1. kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{'\t'}{.status.startTime}{'\n'}{end}"
  2. kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{\"\t\"}{.status.startTime}{\"\n\"}{end}"

Note:

JSONPath regular expressions are not supported. If you want to match using regular expressions, you can use a tool such as jq.

  1. # kubectl does not support regular expressions for JSONpath output
  2. # The following command does not work
  3. kubectl get pods -o jsonpath='{.items[?(@.metadata.name=~/^test$/)].metadata.name}'
  4. # The following command achieves the desired result
  5. kubectl get pods -o json | jq -r '.items[] | select(.metadata.name | test("test-")).spec.containers[].image'