Skip to content

Commit 602a0b0

Browse files
authored
Merge pull request #49874 from sftim/20250223_improve_jsonpath_doc
Improve JSONPath reference
2 parents 0aa07a3 + 9195e20 commit 602a0b0

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

content/en/docs/reference/kubectl/jsonpath.md

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,34 @@
22
title: JSONPath Support
33
content_type: concept
44
weight: 40
5+
math: true
56
---
67

78
<!-- overview -->
8-
Kubectl supports JSONPath template.
9+
The {{< glossary_tooltip term_id="kubectl" text="kubectl" >}} tool supports JSONPath templates as an output format.
910

1011

1112
<!-- body -->
1213

13-
JSONPath template is composed of JSONPath expressions enclosed by curly braces {}.
14+
A _JSONPath template_ is composed of JSONPath expressions enclosed by curly braces: `{` and `}`.
1415
Kubectl uses JSONPath expressions to filter on specific fields in the JSON object and format the output.
1516
In addition to the original JSONPath template syntax, the following functions and syntax are valid:
1617

1718
1. Use double quotes to quote text inside JSONPath expressions.
1819
2. Use the `range`, `end` operators to iterate lists.
19-
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`.
20+
3. Use negative slice indices to step backwards through a list.
21+
Negative indices do _not_ "wrap around" a list and are valid as long as \\( ( - index + listLength ) \ge 0 \\).
2022

2123
{{< note >}}
2224

2325
- The `$` operator is optional since the expression always starts from the root object by default.
2426

25-
- The result object is printed as its String() function.
27+
- The result object is printed as its `String()` function.
2628

2729
{{< /note >}}
2830

31+
## Functions in Kubernetes JSONPath {#functions}
32+
2933
Given the JSON input:
3034

3135
```json
@@ -70,6 +74,7 @@ Given the JSON input:
7074
}
7175
```
7276

77+
{{< table caption="Functions, their parameters, an example invocation, and the result" >}}
7378
Function | Description | Example | Result
7479
--------------------|------------------------------|-----------------------------------------------------------------|------------------
7580
`text` | the plain text | `kind is {.kind}` | `kind is List`
@@ -83,6 +88,9 @@ Function | Description | Example
8388
`range`, `end` | iterate list | `{range .items[*]}[{.metadata.name}, {.status.capacity}] {end}` | `[127.0.0.1, map[cpu:4]] [127.0.0.2, map[cpu:8]]`
8489
`''` | quote interpreted string | `{range .items[*]}{.metadata.name}{'\t'}{end}` | `127.0.0.1 127.0.0.2`
8590
`\` | escape termination character | `{.items[0].metadata.labels.kubernetes\.io/hostname}` | `127.0.0.1`
91+
{{< /table >}}
92+
93+
## Using JSONPath expressions with kubectl {#use-with-kubectl}
8694

8795
Examples using `kubectl` and JSONPath expressions:
8896

@@ -105,7 +113,7 @@ kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{\"\t\"}{.status.
105113
```
106114
{{< /note >}}
107115

108-
{{< note >}}
116+
## Regular expressions in JSONPath
109117

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

@@ -117,4 +125,3 @@ kubectl get pods -o jsonpath='{.items[?(@.metadata.name=~/^test$/)].metadata.nam
117125
# The following command achieves the desired result
118126
kubectl get pods -o json | jq -r '.items[] | select(.metadata.name | test("test-")).metadata.name'
119127
```
120-
{{< /note >}}

0 commit comments

Comments
 (0)