Mockzilla logo

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.

HelperDescriptionExample
andReturns true if all arguments are truthy.{{#if (and $.query.active $.query.admin)}}
orReturns true if any argument is truthy.{{#if (or $.query.id $.query.slug)}}
notInverts a boolean value.{{#if (not $.query.locked)}}
defaultReturns a fallback value if the first is empty.{{default $.query.theme "dark"}}
eq / neqStrict equality check.{{#if (eq $.query.role "admin")}}
gt / ltNumeric comparisons.{{#if (gt $.query.count 5)}}

3. Temporal (Time) Helpers

Mocking dates and expirations is easy with built-in time manipulation.

HelperDescriptionExample
nowCurrent ISO timestamp.{{now}}
dateFormatFormats a date string.{{dateFormat "now" "yyyy-MM-dd"}}
dateAddAdds time to a date.{{dateAdd "now" 30 "days"}}
dateSubSubtracts 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.

HelperDescriptionExample
filterFilters an array by a key/value pair.{{filter $.users "status" "active"}}
sortSorts an array by a specific key.{{sort $.products "price" "desc"}}
sliceReturns a portion of an array.{{slice $.items 0 10}}
joinJoins array elements into a string.{{join $.tags ", "}}
jsonStringifies an object/array.{{{json $.users}}}

5. String & Number Stylists

Format your data to look production-ready.

HelperDescriptionExample
slugifyURL-friendly strings.{{slugify "Hello World!"}} → hello-world
truncateLimits string length with ....{{truncate long_text 50}}
currencyFormats numbers as money.{{currency 123.4 "USD" "en-US"}} → $123.40
toFixedControls decimal precision.{{toFixed 10.555 2}} → 10.56
mathBasic 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

GoalHandlebars 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

SituationResultFix
The value is in the requestInserted as a string—
The value is missingThe 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!)