1. Template Guidelines
  2. JSONata

Template Guidelines

JSONata

Covers common JSONata pitfalls, such as forcing results to always be an array, and how to test JSONata expressions.

JSONata differs from JavaScript in many ways. See JSONata for the language and the JSONata hashtag helper for using it in templates.

Forcing results to always be an array

A common pitfall is that when an expression matches only one element, JSONata returns that element as a single object rather than an array containing that object. This breaks logic that expects to iterate over an array.

To make sure the result is always an array, even when only one element matches, wrap the expression in square brackets: [ expression ].

Example: [stub.data.items[type = "active"]]

Matches Default JSONata output Forced array output
0 [] []
1 {...} [{...}]
many [{...}, {...}] [{...}, {...}]

This keeps output types consistent and prevents errors when later logic expects an array.

Testing JSONata

To check what an expression returns, add a temporary savedata task to an action on your draft template that uses the JSONata hashtag helper, then run the action on a draft stub with sample data and inspect the saved result. Remove the task once you are done.