Skip to content

Commit a55993b

Browse files
authored
Merge pull request #49888 from windsonsea/jsonyh
[zh] Sync kubectl/jsonpath.md
2 parents 3fd6a35 + d01ac14 commit a55993b

File tree

1 file changed

+66
-49
lines changed

1 file changed

+66
-49
lines changed

content/zh-cn/docs/reference/kubectl/jsonpath.md

Lines changed: 66 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,62 @@
22
title: JSONPath 支持
33
content_type: concept
44
weight: 40
5+
math: true
56
---
67
<!--
78
title: JSONPath Support
89
content_type: concept
910
weight: 40
11+
math: true
1012
-->
1113

1214
<!-- overview -->
15+
1316
<!--
14-
Kubectl supports JSONPath template.
17+
The {{< glossary_tooltip term_id="kubectl" text="kubectl" >}} tool supports JSONPath templates as an output format.
1518
-->
16-
kubectl 支持 JSONPath 模板。
17-
19+
{{< glossary_tooltip term_id="kubectl" text="kubectl" >}}
20+
工具支持 JSONPath 模板作为输出格式。
1821

1922
<!-- body -->
2023

2124
<!--
22-
JSONPath template is composed of JSONPath expressions enclosed by curly braces {}.
25+
A _JSONPath template_ is composed of JSONPath expressions enclosed by curly braces: `{` and `}`.
2326
Kubectl uses JSONPath expressions to filter on specific fields in the JSON object and format the output.
2427
In addition to the original JSONPath template syntax, the following functions and syntax are valid:
2528
-->
26-
JSONPath 模板由 {} 包起来的 JSONPath 表达式组成。Kubectl 使用 JSONPath 表达式来过滤 JSON 对象中的特定字段并格式化输出。
29+
**JSONPath 模板**由大括号 `{``}` 包起来的 JSONPath 表达式组成。
30+
kubectl 使用 JSONPath 表达式来过滤 JSON 对象中的特定字段并格式化输出。
2731
除了原始的 JSONPath 模板语法,以下函数和语法也是有效的:
2832

2933
<!--
3034
1. Use double quotes to quote text inside JSONPath expressions.
3135
2. Use the `range`, `end` operators to iterate lists.
32-
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`.
36+
3. Use negative slice indices to step backwards through a list.
37+
Negative indices do _not_ "wrap around" a list and are valid as long as \\( ( - index + listLength ) \ge 0 \\).
3338
-->
3439
1. 使用双引号将 JSONPath 表达式内的文本引起来。
3540
2. 使用 `range``end` 运算符来迭代列表。
36-
3. 使用负片索引后退列表。负索引不会“环绕”列表,并且只要 `-index + listLength> = 0` 就有效。
41+
3. 使用负片索引后退列表。负索引**不会**“环绕”列表,
42+
并且只要 \\( ( - index + listLength ) \ge 0 \\) 就有效。
3743

3844
{{< note >}}
3945
<!--
4046
- The `$` operator is optional since the expression always starts from the root object by default.
4147
42-
- The result object is printed as its String() function.
48+
- The result object is printed as its `String()` function.
4349
-->
4450
- `$` 运算符是可选的,因为默认情况下表达式总是从根对象开始。
4551

46-
- 结果对象将作为其 String() 函数输出。
52+
- 结果对象将作为其 `String()` 函数输出。
4753

4854
{{< /note >}}
4955

56+
<!--
57+
## Functions in Kubernetes JSONPath {#functions}
58+
-->
59+
## Kubernetes JSONPath 中的函数 {#functions}
60+
5061
<!--
5162
Given the JSON input:
5263
-->
@@ -95,33 +106,42 @@ Given the JSON input:
95106
```
96107

97108
<!--
98-
Function | Description | Example | Result
99-
--------------------|------------------------------|-----------------------------------------------------------------|------------------
100-
`text` | the plain text | `kind is {.kind}` | `kind is List`
101-
`@` | the current object | `{@}` | the same as input
102-
`.` or `[]` | child operator | `{.kind}`, `{['kind']}` or `{['name\.type']}` | `List`
103-
`..` | recursive descent | `{..name}` | `127.0.0.1 127.0.0.2 myself e2e`
104-
`*` | wildcard. Get all objects | `{.items[*].metadata.name}` | `[127.0.0.1 127.0.0.2]`
105-
`[start:end:step]` | subscript operator | `{.users[0].name}` | `myself`
106-
`[,]` | union operator | `{.items[*]['metadata.name', 'status.capacity']}` | `127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8]`
107-
`?()` | filter | `{.users[?(@.name=="e2e")].user.password}` | `secret`
108-
`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]]`
109-
`''` | quote interpreted string | `{range .items[*]}{.metadata.name}{'\t'}{end}` | `127.0.0.1 127.0.0.2`
110-
`\` | escape termination character | `{.items[0].metadata.labels.kubernetes\.io/hostname}` | `127.0.0.1`
109+
{{< table caption="Functions, their parameters, an example invocation, and the result" >}}
110+
Function | Description | Example | Result
111+
---------|-------------|---------|-------
112+
`text` | the plain text | `kind is {.kind}` | `kind is List`
113+
`@` | the current object | `{@}` | the same as input
114+
`.` or `[]` | child operator | `{.kind}`, `{['kind']}` or `{['name\.type']}` | `List`
115+
`..` | recursive descent | `{..name}` | `127.0.0.1 127.0.0.2 myself e2e`
116+
`*` | wildcard. Get all objects | `{.items[*].metadata.name}` | `[127.0.0.1 127.0.0.2]`
117+
`[start:end:step]` | subscript operator | `{.users[0].name}` | `myself`
118+
`[,]` | union operator | `{.items[*]['metadata.name', 'status.capacity']}` | `127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8]`
119+
`?()` | filter | `{.users[?(@.name=="e2e")].user.password}` | `secret`
120+
`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]]`
121+
`''` | quote interpreted string | `{range .items[*]}{.metadata.name}{'\t'}{end}` | `127.0.0.1 127.0.0.2`
122+
`\` | escape termination character | `{.items[0].metadata.labels.kubernetes\.io/hostname}` | `127.0.0.1`
123+
{{< /table >}}
124+
-->
125+
{{< table caption="函数、相关参数、调用示例和结果" >}}
126+
函数 | 描述 | 示例 | 结果
127+
----|-----|------|----
128+
`text` | 纯文本 | `kind is {.kind}` | `kind is List`
129+
`@` | 当前对象 | `{@}` | 与输入相同
130+
`.``[]` | 子运算符 | `{.kind}``{['kind']}``{['name\.type']}` | `List`
131+
`..` | 递归下降 | `{..name}` | `127.0.0.1 127.0.0.2 myself e2e`
132+
`*` | 通配符。获取所有对象 | `{.items[*].metadata.name}` | `[127.0.0.1 127.0.0.2]`
133+
`[start:end:step]` | 下标运算符 | `{.users[0].name}` | `myself`
134+
`[,]` | 并集运算符 | `{.items[*]['metadata.name', 'status.capacity']}` | `127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8]`
135+
`?()` | 过滤 | `{.users[?(@.name=="e2e")].user.password}` | `secret`
136+
`range``end` | 迭代列表 | `{range .items[*]}[{.metadata.name}, {.status.capacity}] {end}` | `[127.0.0.1, map[cpu:4]] [127.0.0.2, map[cpu:8]]`
137+
`''` | 引用解释执行字符串 | `{range .items[*]}{.metadata.name}{'\t'}{end}` | `127.0.0.1 127.0.0.2`
138+
`\` | 转义终止符 | `{.items[0].metadata.labels.kubernetes\.io/hostname}` | `127.0.0.1`
139+
{{< /table >}}
140+
141+
<!--
142+
## Using JSONPath expressions with kubectl {#use-with-kubectl}
111143
-->
112-
函数 | 描述 | 示例 | 结果
113-
--------------------|--------------------------|-----------------------------------------------------------------|------------------
114-
`text` | 纯文本 | `kind is {.kind}` | `kind is List`
115-
`@` | 当前对象 | `{@}` | 与输入相同
116-
`.``[]` | 子运算符 | `{.kind}``{['kind']}``{['name\.type']}` | `List`
117-
`..` | 递归下降 | `{..name}` | `127.0.0.1 127.0.0.2 myself e2e`
118-
`*` | 通配符。获取所有对象 | `{.items[*].metadata.name}` | `[127.0.0.1 127.0.0.2]`
119-
`[start:end:step]` | 下标运算符 | `{.users[0].name}` | `myself`
120-
`[,]` | 并集运算符 | `{.items[*]['metadata.name', 'status.capacity']}` | `127.0.0.1 127.0.0.2 map[cpu:4] map[cpu:8]`
121-
`?()` | 过滤 | `{.users[?(@.name=="e2e")].user.password}` | `secret`
122-
`range``end` | 迭代列表 | `{range .items[*]}[{.metadata.name}, {.status.capacity}] {end}` | `[127.0.0.1, map[cpu:4]] [127.0.0.2, map[cpu:8]]`
123-
`''` | 引用解释执行字符串 | `{range .items[*]}{.metadata.name}{'\t'}{end}` | `127.0.0.1 127.0.0.2`
124-
`\` | 转义终止符 | `{.items[0].metadata.labels.kubernetes\.io/hostname}` | `127.0.0.1`
144+
## 通过 kubectl 使用 JSONPath 表达式 {#use-with-kubectl}
125145

126146
<!--
127147
Examples using `kubectl` and JSONPath expressions:
@@ -140,26 +160,27 @@ kubectl get pods -o=jsonpath='{.items[0].metadata.labels.kubernetes\.io/hostname
140160

141161
{{< note >}}
142162
<!--
143-
On Windows, you must _double_ quote any JSONPath template that contains spaces (not single quote as shown above for bash).
144-
This in turn means that you must use a single quote or escaped double quote around any literals in the template. For example:
145-
146-
```cmd
147-
kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{'\t'}{.status.startTime}{'\n'}{end}"
148-
kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{\"\t\"}{.status.startTime}{\"\n\"}{end}"
149-
```
163+
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:
150164
-->
151-
在 Windows 上,对于任何包含空格的 JSONPath 模板,你必须使用双引号(不是上面 bash 所示的单引号)。
165+
在 Windows 上,对于任何包含空格的 JSONPath 模板,你必须使用****引号(不是上面 bash 所示的单引号)。
152166
反过来,这意味着你必须在模板中的所有文字周围使用单引号或转义的双引号。例如:
153167

154168
```cmd
155-
C:\> kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{'\t'}{.status.startTime}{'\n'}{end}"
156-
C:\> kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{\"\t\"}{.status.startTime}{\"\n\"}{end}"
169+
kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{'\t'}{.status.startTime}{'\n'}{end}"
170+
kubectl get pods -o=jsonpath="{range .items[*]}{.metadata.name}{\"\t\"}{.status.startTime}{\"\n\"}{end}"
157171
```
158172
{{< /note >}}
159173

160174
<!--
175+
## Regular expressions in JSONPath
176+
161177
JSONPath regular expressions are not supported. If you want to match using regular expressions, you can use a tool such as `jq`.
178+
-->
179+
## JSONPath 中的正则表达式 {#regular-expression-in-jsonpath}
180+
181+
不支持 JSONPath 正则表达式。如需使用正则表达式进行匹配操作,你可以使用如 `jq` 之类的工具。
162182

183+
<!--
163184
```shell
164185
# kubectl does not support regular expressions for JSONpath output
165186
# The following command does not work
@@ -169,9 +190,6 @@ kubectl get pods -o jsonpath='{.items[?(@.metadata.name=~/^test$/)].metadata.nam
169190
kubectl get pods -o json | jq -r '.items[] | select(.metadata.name | test("test-")).metadata.name'
170191
```
171192
-->
172-
{{< note >}}
173-
不支持 JSONPath 正则表达式。如需使用正则表达式进行匹配操作,你可以使用如 `jq` 之类的工具。
174-
175193
```shell
176194
# kubectl 的 JSONpath 输出不支持正则表达式
177195
# 下面的命令不会生效
@@ -180,4 +198,3 @@ kubectl get pods -o jsonpath='{.items[?(@.metadata.name=~/^test$/)].metadata.nam
180198
# 下面的命令可以获得所需的结果
181199
kubectl get pods -o json | jq -r '.items[] | select(.metadata.name | test("test-")).metadata.name'
182200
```
183-
{{< /note >}}

0 commit comments

Comments
 (0)