Reputation: 51
I'm trying to extract an object from an array if it matches a variable value. The user passes an author name stored in a variable named vars.author. I then want to match that value with the the author keys in the array and return the object that contains that value.
{
"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"
}
]
}
}
So assuming vars.author contains the value "Corets, Eva", I would like to return the following:
{
"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"
},
I have tried a variation of the below, but nothing has worked. I have looked in here, but nothing matches exactly. If anyone can refer me to a question that has already been asked here, or can help me themselves, I'd appreciate it. Thank you for your help.
payload.catalog.book
filter ((item, index) -> item.author == vars.author)
map ((item, index) -> item.author)
UPDATE: Found the solution thanks to the responses below. Thank you everyone.
payload filter ($.author == vars.author) map {
author: $.author,
title: $.title,
genre: $.genre,
price: $.price,
publish_date: $.publish_date,
description: $.description,
id: $."_id"
}
Upvotes: 0
Views: 743
Reputation: 467
As Aled mentioned output is not an object and it is an array. For your requirement to return the object that contains that value, you will have to use foreach in your flow and then send the each object.
And also as Karthik said you do not have to map below will still work,
payload.catalog.book filter ((item, index) -> item.author == vars.author)
Output :
[ { "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" } ]
Upvotes: 2
Reputation: 25812
The response seems to be your script without the final map. That will return an array of the items of the input array that match the condition on the author name.
Note that the output is not an object but an array because there are multiple possible matches. You could transform it to an object for example using reduce, but you would need to add a key to each value.
Upvotes: 0