use*_*243 2 search json substring contains jmespath
尽管进行了大量研究,我还是找不到解决方案。我被 contains 函数困住了。我有这个 Json 文件:
{
"from": "Api",
"success": true,
"message": "",
"errors": [],
"data": {
"operations": [
{
"IDOperation": 100,
"DateEcriture": "2019-01-02",
"Comment": "Invoice Nh5 numero 152",
"sous_operations": []
},
{
"IDOperation": 101,
"DateEcriture": "2019-01-02",
"Comment": "one other thing",
"sous_operations": []
},
{
"IDOperation":102,
"DateEcriture": "2019-01-02",
"Comment": "an other thing",
"sous_operations": [{"ID-sous-Operation": 103,
"DateEcriture": "2019-01-02",
"Comment": "Invoice Nh15 numero 341"}]
}]
}
}
Run Code Online (Sandbox Code Playgroud)
我想过滤“注释”字段中包含“发票”一词的对象以获得以下结果:
{"operations": [
{
"IDOperation": 100,
"DateEcriture": "2019-01-02",
"Comment": "Invoice Nh5 numero 152"
},
{
"IDOperation": 103,
"DateEcriture": "2019-01-02",
"Comment": "Invoice Nh15 numero 341"
}]
}
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助
您还没有说明您在哪一部分遇到困难。我猜它正在处理嵌套子操作,因为这对我来说似乎是最难和最不明显的部分,但我会尽力涵盖所有内容。
这是我的假设:
data。datafield 始终是一个带有 field 的对象operations。operations始终是一个数组。operations都有相同的四个字段:IDOperation、DateEcriture、Comment、sous_operations。sous_operations始终是一个数组。sous_operations具有相同的三个字段:ID-sous-Operation(!)DateEcriture和Comment。Comment都是字符串。Comment。ID-sous-Operation为IDOperation.operations该字段是所选和转换操作的数组。我认为这符合你的要求:
{
operations:
data.operations|
map(
&[
[
{
IDOperation:IDOperation,
DateEcriture:DateEcriture,
Comment:Comment
}
],
map(
&{
IDOperation:"ID-sous-Operation",
DateEcriture:DateEcriture,
Comment:Comment
},
sous_operations
)
],
@
)|
[][]|
[?contains(Comment,`"Invoice"`)]
}
Run Code Online (Sandbox Code Playgroud)
首先,我们用一个二元数组替换每个操作。第一个成员是一个单元素数组,包含操作的字段,但不包含其子操作。第二个成员是所有操作的子操作的数组。(此时我们还重命名子操作 ID 字段。)
所以现在我们的操作是一个由简化操作数组组成的(两个元素)数组组成的数组。我们使用展平运算符两次来获得一个简单的单层数组。最后我们简单地使用 contains 方法对其进行过滤。
这是输出:
$ jp --filename input1.json --expr-file filter.jmespath
{
"operations": [
{
"Comment": "Invoice Nh5 numero 152",
"DateEcriture": "2019-01-02",
"IDOperation": 100
},
{
"Comment": "Invoice Nh15 numero 341",
"DateEcriture": "2019-01-02",
"IDOperation": 103
}
]
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3559 次 |
| 最近记录: |