How do I specify a value nested in an array in a Mockoon rule?

826 views Asked by At

I'm trying to mock different responses in mockoon based on value1 in the following body request:

{
"foo":"bar",
"records" : [{
  "key1" : "value1",
  "key2" : "value2"
  }
]
}

The logical approach would have been to point as records[0].key1 to compare the value, but that does not seem to work.

2

There are 2 answers

0
Dri On

A little reading goes a long way.

The answer for my case is as follows:

records.0.key1

This is from the Adding response rules from the Mockoon documentation, https://mockoon.com/docs/latest/route-responses/dynamic-rules/:

body: keep empty to match against the full raw body content. use a path to access one of its properties. The syntax is based on the object-path library like users.0.name. This is compatible with request's bodies of Content-Type application/json, application/x-www-form-urlencoded, multipart/form-data, application/xml, application/soap+xml or text/xml. Properties containing dots are supported by escaping the dots: key.key.with.dot. Please note that XML bodies are parsed using xml-js package. Refer to this page or the package documentation for more information on how the XML is parsed and how to fetch specific properties. Please also note that multipart/form-data only supports fields. Uploaded files will be ignored.

0
255kb - Mockoon On

Aside from the dot notation records.0.key1, Mockoon also supports JSONPath. So, you should be able to fetch an array element by using the following syntax:

$.records[0]