Overview
The Jsonnet element allows you to embed Jsonnet templates directly within your Elemental notification structure. Jsonnet is a data templating language designed to generate JSON output, making it ideal for programmatically creating structured content, avoiding repetition, and building complex data structures. When to use:- Generate dynamic JSON structures based on data
- Create reusable template logic with functions and variables
- Build complex nested objects programmatically
- Avoid repetitive JSON structures
- Generate locale-specific content programmatically
Basic Example
Fields
The type of element. For Jsonnet elements, this value must be
"jsonnet".The Jsonnet template code. This is a string containing valid Jsonnet syntax that will be evaluated to generate JSON output. The template has access to the message data via Jsonnet’s standard variable access.
Region-specific Jsonnet templates. The object keys are locale codes (e.g.,
"es", "fr"), and values are Jsonnet template strings that will be used for that locale. See Locales documentation for more details.An array of channel names. The Jsonnet element will only be rendered for the specified channels. See Control Flow documentation for details.
Jsonnet Language Basics
Jsonnet is a data templating language that extends JSON with features like:- Variables:
local name = "Alice"; - Functions:
local greet(name) = "Hello " + name; - Object composition: Merge objects with
+ - Conditionals:
if condition then value1 else value2 - Loops:
[x * 2 for x in [1, 2, 3]] - Imports:
local lib = import "lib.jsonnet";
For complete Jsonnet language documentation, see the official Jsonnet documentation.
Examples & Variants
Simple Object Generation
Generate a simple JSON object:Using Functions
Create reusable functions:Conditional Content
Generate different content based on conditions:Array Generation
Generate arrays programmatically:Localized Templates
Provide different Jsonnet templates for different locales:Complex Nested Structures
Build complex nested objects:Channel-Specific Jsonnet
Render Jsonnet only for specific channels:Best Practices
- Keep templates readable: Use multi-line strings in your code for complex templates
- Use functions for reusability: Extract common logic into functions
- Validate output: Ensure your Jsonnet templates produce valid JSON
- Test templates: Test Jsonnet templates independently before embedding in Elemental
- Consider alternatives: For simple content, Handlebars in Text elements may be simpler
- Error handling: Jsonnet syntax errors will cause rendering to fail, so test thoroughly
Channel Support
- Email: ✅ Full support
- Push: ✅ Supported (JSON output can be used in push payloads)
- SMS: ⚠️ Limited support (JSON output may need to be stringified)
- Inbox: ✅ Full support
Related Elements
- Text Element - For simple text content with Handlebars
- HTML Element - For raw HTML content
- Group Element - For grouping Jsonnet with other elements
- Control Flow - For conditional rendering
- Locales - For locale-specific content