mockingmockoon

Mockoon accessing data bucket array element


I want to generate a response by getting the object associated with a query id out of a data bucket containing an array of objects with key id

My solution is as follows:

{{setVar 'myId' (queryParam 'id')}}
{{setVar 'jsonPath' (concat '$.[?(@.id===' (getVar 'myId') ')]') }}
{{setVar 'bucket'  (data 'DataBucket' @jsonPath)  }}     
{
  id: {{@myId}},
  path: "{{{@jsonPath}}}"
  dataBucket: {{@bucket}}
}

I found I had to construct the JSONPath` string as I couldn't seem to directly create the string with the id. Is there a better approach?


Solution

  • There is no better approach than using a concat as the JSONPath will not get parsed for variable presence. Also, Handlebars is logic less, which mandate the use of helpers like concat instead of having simpler symbols like +.