Skip to content

TemplatedObject 1.0.0

Overview

Function Beginner

Version Source

Available Versions: 1.0.0 (current) | 1.0.0

Description

A block that takes a Jinja2 template string, fills the template, and then parses the result into a JSON object.

Configuration Options

NameData TypeDescriptionDefault Value
templated_jsonstrThe Jinja2 template string that is formatted and parsed to JSONNone (Required)

Inputs

NameData TypeDescription
**inputsAnnotated[Any, Metadata()]Objects passed to the Jinja2 template for variable substitution

Outputs

NameData TypeDescription
jsondict[str, Any]The parsed JSON object result from the rendered template

Version History

  • 1.0.0 (Current) - Native implementation
  • 1.0.0 - Sdk implementation

Examples

# Create a simple user profile JSON from template
steps:
  - name: create_user_profile
    block: TemplatedObject
    config:
      templated_json: |
        {
          "user_id": "{{ user_data.id }}",
          "name": "{{ user_data.name }}",
          "email": "{{ user_data.email }}",
          "created_at": "{{ timestamp }}"
        }
    inputs:
      user_data:
        id: "usr_12345"
        name: "John Doe"
        email: "john.doe@example.com"
      timestamp: "2024-07-21T10:30:00Z"

Error Handling

TemplateError

Error Code
ValueError: Error in rendering Jinja2 template
Common Cause
Invalid Jinja2 syntax in template string or undefined variables
Solution
Verify template syntax and ensure all referenced variables are provided in inputs

JSONDecodeError

Error Code
ValueError: Error in parsing rendered template to JSON
Common Cause
Template renders to invalid JSON format
Solution
Check rendered output in error message - ensure proper JSON escaping and structure

FAQ

How does the AutoJson wrapper system work?

The block automatically wraps input objects with AutoJson wrappers that enable dot-notation access (e.g., user.name) and ensure proper JSON serialization when converted to strings in templates. This allows complex nested object access while maintaining valid JSON output.

Can I include arrays and nested objects in my template?

Yes, use double curly braces without quotes for arrays and objects: {{ my_array }} or {{ nested_object }}. The AutoJson system will automatically serialize them as valid JSON when the template is rendered.

What happens if my template produces invalid JSON?

The block will raise a JSONDecodeError with the rendered output included in the error message. Common issues include unescaped quotes in string values or missing commas between object properties.

How do I handle special characters in string values?

String values are automatically JSON-escaped when using the AutoJson wrapper system. For manual escaping, ensure quotes and backslashes are properly escaped in your template or input data.