【发布时间】:2022-02-04 20:32:08
【问题描述】:
我正在尝试从数组中提取与变量值匹配的对象。用户传递存储在名为 vars.author 的变量中的作者姓名。然后我想将该值与数组中的作者键匹配并返回包含该值的对象。
{
"catalog": {
"book": [
{
"author": "Ralls, Kim",
"title": "Midnight Rain",
"genre": "Fantasy",
"price": "5.95",
"publish_date": "2000-12-16",
"description": "A former architect battles corporate zombies, \n an evil sorceress, and her own childhood to become queen \n of the world.",
"_id": "bk102"
},
{
"author": "Corets, Eva",
"title": "Maeve Ascendant",
"genre": "Fantasy",
"price": "5.95",
"publish_date": "2000-11-17",
"description": "After the collapse of a nanotechnology \n society in England, the young survivors lay the \n foundation for a new society.",
"_id": "bk103"
},
{
"author": "Corets, Eva",
"title": "Oberon's Legacy",
"genre": "Fantasy",
"price": "5.95",
"publish_date": "2001-03-10",
"description": "In post-apocalypse England, the mysterious \n agent known only as Oberon helps to create a new life \n for the inhabitants of London. Sequel to Maeve \n Ascendant.",
"_id": "bk104"
},
{
"author": "Galos, Mike",
"title": "Visual Studio 7: A Comprehensive Guide",
"genre": "Computer",
"price": "49.95",
"publish_date": "2001-04-16",
"description": "Microsoft Visual Studio 7 is explored in depth,\n looking at how Visual Basic, Visual C++, C#, and ASP+ are \n integrated into a comprehensive development \n environment.",
"_id": "bk112"
}
]
}
}
所以假设 vars.author 包含值“Corets, Eva”,我想返回以下内容:
{
"author": "Corets, Eva",
"title": "Maeve Ascendant",
"genre": "Fantasy",
"price": "5.95",
"publish_date": "2000-11-17",
"description": "After the collapse of a nanotechnology \n society in England, the young survivors lay the \n foundation for a new society.",
"_id": "bk103"
},
{
"author": "Corets, Eva",
"title": "Oberon's Legacy",
"genre": "Fantasy",
"price": "5.95",
"publish_date": "2001-03-10",
"description": "In post-apocalypse England, the mysterious \n agent known only as Oberon helps to create a new life \n for the inhabitants of London. Sequel to Maeve \n Ascendant.",
"_id": "bk104"
},
我尝试了以下变体,但没有任何效果。我看过这里,但没有完全匹配。如果有人可以向我推荐这里已经提出的问题,或者可以自己帮助我,我将不胜感激。感谢您的帮助。
payload.catalog.book
filter ((item, index) -> item.author == vars.author)
map ((item, index) -> item.author)
更新: 感谢以下回复,找到了解决方案。谢谢大家。
payload filter ($.author == vars.author) map {
author: $.author,
title: $.title,
genre: $.genre,
price: $.price,
publish_date: $.publish_date,
description: $.description,
id: $."_id"
}
【问题讨论】:
-
您似乎错过了输出数组上的括号。它可以是一个对象,因为它缺少每个项目的键。一个对象不能有两个用逗号分隔的对象。
-
@ammanbesaw 。正如aled 提到的,您的输出将是对象数组,但是为什么您需要一个地图?只使用过滤器不能完成你的工作吗?
标签: json mule dataweave mulesoft