使用 jmes 查询过滤嵌套数组
Filter nested array using jmes query
我必须得到 'John' 在 'sales' 部门工作的公司名称。我的 JSON
看起来像这样:
[
{
"name" : "John",
"company" : [{
"name" : "company1",
"department" : "sales"
},
{
"name" : "company2",
"department" : "backend"
},
{
"name" : "company3",
"department" : "sales"
}
],
"phone" : "1234"
}
]
而我的jmesquery是这样的:
jmesquery: "[? name=='John'].company[? department=='sales'].{Company: name}"
但是通过这个查询,我得到了一个 null
数组。
这是因为您的第一个过滤器 [?name=='John']
正在创建一个 projection, and more specifically a filter projection,您必须重置它才能进一步过滤它。
可以使用 pipes.
来重置投影
Projections are an important concept in JMESPath. However, there are times when projection semantics are not what you want. A common scenario is when you want to operate of the result of a projection rather than projecting an expression onto each element in the array.
For example, the expression people[*].first
will give you an array containing the first names of everyone in the people array. What if you wanted the first element in that list? If you tried people[*].first[0]
that you just evaluate first[0]
for each element in the people array, and because indexing is not defined for strings, the final result would be an empty array, []
. To accomplish the desired result, you can use a pipe expression, <expression> | <expression>
, to indicate that a projection must stop.
来源:https://jmespath.org/tutorial.html#pipe-expressions
所以,这将是您查询的第一步:
[?name=='John'] | [].company[?department=='sales'].{Company: name}
这样说,这还是以数组数组结尾:
[
[
{
"Company": "company1"
},
{
"Company": "company3"
}
]
]
因为您最终可以在 sales department
中有多个名为 John
的人。
因此,一个数组用于 users
,另一个数组用于 companies/departments
.
为了解决这个问题,您可以使用 flatten operator:[]
。
所以我们结束于:
[?name=='John'] | [].company[?department=='sales'].{Company: name} []
给出:
[
{
"Company": "company1"
},
{
"Company": "company3"
}
]
我必须得到 'John' 在 'sales' 部门工作的公司名称。我的 JSON 看起来像这样:
[
{
"name" : "John",
"company" : [{
"name" : "company1",
"department" : "sales"
},
{
"name" : "company2",
"department" : "backend"
},
{
"name" : "company3",
"department" : "sales"
}
],
"phone" : "1234"
}
]
而我的jmesquery是这样的:
jmesquery: "[? name=='John'].company[? department=='sales'].{Company: name}"
但是通过这个查询,我得到了一个 null
数组。
这是因为您的第一个过滤器 [?name=='John']
正在创建一个 projection, and more specifically a filter projection,您必须重置它才能进一步过滤它。
可以使用 pipes.
Projections are an important concept in JMESPath. However, there are times when projection semantics are not what you want. A common scenario is when you want to operate of the result of a projection rather than projecting an expression onto each element in the array. For example, the expression
people[*].first
will give you an array containing the first names of everyone in the people array. What if you wanted the first element in that list? If you triedpeople[*].first[0]
that you just evaluatefirst[0]
for each element in the people array, and because indexing is not defined for strings, the final result would be an empty array,[]
. To accomplish the desired result, you can use a pipe expression,<expression> | <expression>
, to indicate that a projection must stop.
来源:https://jmespath.org/tutorial.html#pipe-expressions
所以,这将是您查询的第一步:
[?name=='John'] | [].company[?department=='sales'].{Company: name}
这样说,这还是以数组数组结尾:
[
[
{
"Company": "company1"
},
{
"Company": "company3"
}
]
]
因为您最终可以在 sales department
中有多个名为 John
的人。
因此,一个数组用于 users
,另一个数组用于 companies/departments
.
为了解决这个问题,您可以使用 flatten operator:[]
。
所以我们结束于:
[?name=='John'] | [].company[?department=='sales'].{Company: name} []
给出:
[
{
"Company": "company1"
},
{
"Company": "company3"
}
]