Dynamic Data Syntax
Use templates in a Manual JSON response or in string values generated by a Dynamic Response JSON Schema. Mockzilla resolves simple paths with type-preserving interpolation and uses Handlebars for helpers and blocks.
1. The Handlebars-First Philosophy
For Dynamic Response, enter a valid JSON Schema in the editor, not a raw response template. Put templates in schema values such as const; Mockzilla generates data first and then interpolates those values. Manual JSON can contain templates directly. Workflow responses and effects have their own context.
2. Advanced Logic Helpers
Control the flow of your data with standard and boolean logic.
| Helper | Description | Example |
|---|---|---|
and | Returns true if all arguments are truthy. | {{#if (and $.query.active $.query.admin)}} |
or | Returns true if any argument is truthy. | {{#if (or $.query.id $.query.slug)}} |
not | Inverts a boolean value. | {{#if (not $.query.locked)}} |
default | Returns a fallback value if the first is empty. | {{default $.query.theme "dark"}} |
eq / neq | Strict equality check. | {{#if (eq $.query.role "admin")}} |
gt / lt | Numeric comparisons. | {{#if (gt $.query.count 5)}} |
3. Temporal (Time) Helpers
Mocking dates and expirations is easy with built-in time manipulation.
| Helper | Description | Example |
|---|---|---|
now | Current ISO timestamp. | {{now}} |
dateFormat | Formats a date string. | {{dateFormat "now" "yyyy-MM-dd"}} |
dateAdd | Adds time to a date. | {{dateAdd "now" 30 "days"}} |
dateSub | Subtracts time from a date. | {{dateSub "now" 1 "hours"}} |
Units supported:
years,months,weeks,days,hours,minutes,seconds.
4. Collection Helpers
Use collection helpers on array fields produced by a dynamic schema, such as a generated users or items field.
| Helper | Description | Example |
|---|---|---|
filter | Filters an array by a key/value pair. | {{filter $.users "status" "active"}} |
sort | Sorts an array by a specific key. | {{sort $.products "price" "desc"}} |
slice | Returns a portion of an array. | {{slice $.items 0 10}} |
join | Joins array elements into a string. | {{join $.tags ", "}} |
json | Stringifies an object/array. | {{{json $.users}}} |
5. String & Number Stylists
Format your data to look production-ready.
| Helper | Description | Example |
|---|---|---|
slugify | URL-friendly strings. | {{slugify "Hello World!"}} → hello-world |
truncate | Limits string length with .... | {{truncate long_text 50}} |
currency | Formats numbers as money. | {{currency 123.4 "USD" "en-US"}} → $123.40 |
toFixed | Controls decimal precision. | {{toFixed 10.555 2}} → 10.56 |
math | Basic arithmetic (+ - * / %). | {{math $.query.val "+" 1}} |
6. Power Patterns (Masterclass)
Echo request values in a response
Enter this in Manual JSON. In a served mock, path captures and query parameters are available in the response context. Query values are strings. For Dynamic Response, put each template in a schema property, such as "page": { "type": "string", "const": "{{$.query.page}}" }.
{
"productId": "{{$.params.[0]}}",
"category": "{{$.query.category}}",
"page": "{{$.query.page}}",
"generatedAt": "{{now}}"
}
Build a response from request values
In a manual response template, return a different permission list based on a request header or query value. This raw template is not valid JSON or a JSON Schema. The editor requires valid JSON for Manual JSON, so use it only where a raw response template is accepted, such as a text response or a workflow template.
{
"user": "{{faker.person.fullName}}",
"permissions": {{#if (or (eq $.headers.[x-role] "admin") (eq $.query.user_id "1"))}}
["read", "write", "delete", "sudo"]
{{else}}
["read"]
{{/if}},
"debug_info": {
"requested_at": "{{now}}",
"trace_id": "{{faker.string.uuid}}"
}
}
7. Smart Context Reference
| Goal | Handlebars Syntax |
|---|---|
| Query Param | {{$.query.id}} |
| Path Segment | {{$.params.[0]}} |
| Headers | {{$.headers.[user-agent]}} |
For ordinary served mock responses, the available request context is query parameters, wildcard captures, and headers. The request body is not available for response interpolation; use Echo Request Body when you need to return it unchanged.
Header names are lowercase. Use brackets for numeric keys and hyphenated names:
{{$.params.[0]}}, {{$.headers.[x-api-key]}}.
Missing values and types
| Situation | Result | Fix |
|---|---|---|
| The value is in the request | Inserted as a string | — |
| The value is missing | The template text stays in the output, for example "{{$.query.page}}" | {{default $.query.page "1"}}, or require the key in Query Parameters |
| A number is needed | "const": "{{$.query.page}}" returns "2" | {{math $.query.page "+" 0}} returns 2 |
math with a missing value | "NaN" | Require the key or add a default |
See the Dynamic Wildcard Mocks guide for a complete example.
8. Type Preservation Mastery
When a template produces valid JSON, Mockzilla parses the result. A whole-value interpolation can preserve the resolved type; text surrounding a value produces a string. The following is a raw response template, not a JSON Schema; the Manual JSON editor cannot save it because it is not valid JSON:
{
"isAdmin": {{#if (eq $.query.admin "true")}}true{{else}}false{{/if}},
"price": {{math $.query.base "*" 1.1}}
}
(Notice: Use triple braces {{{json ...}}} to prevent Handlebars from HTML-escaping JSON characters!)
