I'm trying to mock a HHTP post request/response with Mockoon. The request sends a list of IDs like so:
{
"ids": [
"d29688fee0d24ed9889f1d1d17062166",
"635a9e8cdaf340bc97c5608193eca946",
"f552e60b010641eba01ea33e2acf9bfd"
]
}
Then I need in Mockoon to filter an existing Data Bucket by all of the IDs sent through the request.
I can filter it manually by one ID like so:
{{{ stringify (
filter (dataRaw 'accounts')
(object id = 'd29688fee0d24ed9889f1d1d17062166')
) }}}
Or by multiple ones:
{{{ stringify (
filter (dataRaw 'accounts')
(object id = 'd29688fee0d24ed9889f1d1d17062166')
(object id = '635a9e8cdaf340bc97c5608193eca946')
) }}}
But I simply can't figure out how to iterate over the incoming IDs and apply the filtering.
The only thing I was able to achieve was using each
over the array from the body and manually adding commas and wrap the result as an array in square brackets:
[
{{#each (bodyRaw 'ids')}}
{{{ stringify (
oneOf (filter (dataRaw 'accounts') (object id = this ) )
) }}}
{{#unless @last}},{{/unless}}
{{/each}}
]
It's working but I guess there should be a more elegant way to achieve it.
Thanks
There is currently no other way. We would require more array helpers like includes
to iterate over the data bucket array instead.
In v5.0.0, there will be built-in support for JSONPath in the dataRaw helper. This will allow you to build more complex lookup paths as JSONPath supports filtering. We will also add data buckets filtering when used with CRUD routes, with query params: GET /users?id=xxxx which may also help.