从 JMESPath 表达式中删除方括号
Remove square brackets from JMESPath expression
我关注JSON:
{
"694992": [
{
"domain": "example.com",
"domain_id": 49392164,
"data": [
{
"category": "Main",
"category_id": 77133,
"data": [
{
"keyword": "sofort",
"key_id": 25963217,
"data": {
"9242": [
27,
"https://www.example.com/sofort",
false,
false,
1
]
}
},
{
"keyword": "das",
"key_id": 32325213,
"data": {
"9242": [
23,
"https://www.example.com/das",
false,
false,
1
]
}
},
{
"keyword": "wiki",
"key_id": 32325317,
"data": {
"9242": [
44,
"https://www.example.com/wiki",
false,
false,
1
]
}
}
]
}
]
}
]
}
我想使用 JMESPath 表达式从 JSON 中提取一些数据并以以下形式获取它:
+---------+--------+--------------------------------+
| Keyword | Number | URL |
+---------+--------+--------------------------------+
| sofort | 27 | https://www.example.com/sofort |
+---------+--------+--------------------------------+
| das | 23 | https://www.example.com/das |
+---------+--------+--------------------------------+
| wiki | 44 | https://www.example.com/wiki |
+---------+--------+--------------------------------+
我使用以下 JMESPath 表达式:
*[].data[].data[].{Keyword: keyword, Number: data.*[0], URL: data.*[1]}
但我的输出是这样的
+---------+--------+----------------------------------+
| Keyword | Number | URL |
+---------+--------+----------------------------------+
| sofort | [27] | [https://www.example.com/sofort] |
+---------+--------+----------------------------------+
| das | [23] | [https://www.example.com/das] |
+---------+--------+----------------------------------+
| wiki | [44] | [https://www.example.com/wiki] |
+---------+--------+----------------------------------+
如何从 Number 和 URL 中删除方括号?
这是因为 data.*
中的 *
可以 return 多个值,例如来自:
{
"keyword": "wiki",
"key_id": 32325317,
"data": {
"9242": [
44,
"https://www.example.com/wiki",
false,
false,
1
],
"9243": [
44,
"https://www.example.com/wiki",
false,
false,
1
]
}
}
这是完全有效的 JSON。
如果您确定您的 data
将始终只包含一个元素,您可以使用 pipe expression: |
停止投影并帮助您从数组中提取第一个元素。
来自文档:
Pipe expression are useful for stopping projections. They can also be used to group expressions.
来源:https://jmespath.org/examples.html#pipes
这个例子也说明了这一点:https://jmespath.org/examples.html#working-with-nested-data
所以你的表达最终是:
*[].data[].data[].{Keyword: keyword, Number: data.*[0] | [0], URL: data.*[1] | [0]}
这给出了预期:
[
{
"Keyword": "sofort",
"Number": 27,
"URL": "https://www.example.com/sofort"
},
{
"Keyword": "das",
"Number": 23,
"URL": "https://www.example.com/das"
},
{
"Keyword": "wiki",
"Number": 44,
"URL": "https://www.example.com/wiki"
}
]
我关注JSON:
{
"694992": [
{
"domain": "example.com",
"domain_id": 49392164,
"data": [
{
"category": "Main",
"category_id": 77133,
"data": [
{
"keyword": "sofort",
"key_id": 25963217,
"data": {
"9242": [
27,
"https://www.example.com/sofort",
false,
false,
1
]
}
},
{
"keyword": "das",
"key_id": 32325213,
"data": {
"9242": [
23,
"https://www.example.com/das",
false,
false,
1
]
}
},
{
"keyword": "wiki",
"key_id": 32325317,
"data": {
"9242": [
44,
"https://www.example.com/wiki",
false,
false,
1
]
}
}
]
}
]
}
]
}
我想使用 JMESPath 表达式从 JSON 中提取一些数据并以以下形式获取它:
+---------+--------+--------------------------------+
| Keyword | Number | URL |
+---------+--------+--------------------------------+
| sofort | 27 | https://www.example.com/sofort |
+---------+--------+--------------------------------+
| das | 23 | https://www.example.com/das |
+---------+--------+--------------------------------+
| wiki | 44 | https://www.example.com/wiki |
+---------+--------+--------------------------------+
我使用以下 JMESPath 表达式:
*[].data[].data[].{Keyword: keyword, Number: data.*[0], URL: data.*[1]}
但我的输出是这样的
+---------+--------+----------------------------------+
| Keyword | Number | URL |
+---------+--------+----------------------------------+
| sofort | [27] | [https://www.example.com/sofort] |
+---------+--------+----------------------------------+
| das | [23] | [https://www.example.com/das] |
+---------+--------+----------------------------------+
| wiki | [44] | [https://www.example.com/wiki] |
+---------+--------+----------------------------------+
如何从 Number 和 URL 中删除方括号?
这是因为 data.*
中的 *
可以 return 多个值,例如来自:
{
"keyword": "wiki",
"key_id": 32325317,
"data": {
"9242": [
44,
"https://www.example.com/wiki",
false,
false,
1
],
"9243": [
44,
"https://www.example.com/wiki",
false,
false,
1
]
}
}
这是完全有效的 JSON。
如果您确定您的 data
将始终只包含一个元素,您可以使用 pipe expression: |
停止投影并帮助您从数组中提取第一个元素。
来自文档:
Pipe expression are useful for stopping projections. They can also be used to group expressions.
来源:https://jmespath.org/examples.html#pipes
这个例子也说明了这一点:https://jmespath.org/examples.html#working-with-nested-data
所以你的表达最终是:
*[].data[].data[].{Keyword: keyword, Number: data.*[0] | [0], URL: data.*[1] | [0]}
这给出了预期:
[
{
"Keyword": "sofort",
"Number": 27,
"URL": "https://www.example.com/sofort"
},
{
"Keyword": "das",
"Number": 23,
"URL": "https://www.example.com/das"
},
{
"Keyword": "wiki",
"Number": 44,
"URL": "https://www.example.com/wiki"
}
]