Skip to content

Useful Jinja Filters and Extension in APIO

Key Filters

  • tojson: Converts a dictionary to a JSON Object.
jinja
{{ data | tojson }}
  • json: Converts a JSON string into a dictionary for further manipulation.
jinja
{{ context.response | json }}
  • pp_dict: Formats a dictionary into a pretty-printed JSON Object.
jinja
{{ data | pp_dict }}
  • combine: Merges dictionaries.
jinja
{{ request.body | combine(context.response | json) }}
  • dict_filter: Filters dictionary entries by key.
jinja
{{ request.body | dict_filter("firstName") }}
  • default: It ensures a value defaults to something else if it’s undefined or falsy.
jinja
{{ data | default(default_value) }}
  • If data is falsy or undefined, it uses default_value instead. Examples of default_value: "default value", 42, [1, 2, 3].
  • If data is truthy, its value is used directly.

Using Python-like Jinja filters

Jinja templates in APIO allow you to leverage Python-like commands and filters to manipulate data dynamically. This section explains how to use these commands, including an example of decoding Base64-encoded strings.


Example: Decoding Base64 Strings

Base64 encoding is commonly used to encode data for safe transmission. You can use the built-in b64decode filter in Jinja to decode a Base64-encoded string stored in a context variable.

Basic Decoding To decode a Base64-encoded string:

jinja
{{ coded_string | b64decode }}
  • Input Example:

    json
    {
        "context": {
            "coded_string": "U29tZSB0ZXh0IHRvIGRlY29kZQ=="
        }
    }
  • Template:

    jinja
    Decoded String: {{ coded_string | b64decode }}
  • Output:

    Decoded String: Some text to decode

Handling Incorrect Padding If the Base64-encoded string is padded incorrectly (e.g., missing the necessary = padding at the end), decoding will fail. You can dynamically add the required padding by calculating how much is missing and appending it before decoding:

jinja
{{ (coded_string + (4 - (coded_string | length % 4)) * '=') | b64decode }}
  • Explanation:

    • (4 - (coded_string | length % 4)): Determines the number of padding characters (=) needed to make the string’s length a multiple of 4.
    • coded_string + ...: Appends the required padding (=).
    • | b64decode: Decodes the properly padded string.
  • Example Input:

    json
    {
        "context": {
            "coded_string": "U29tZSB0ZXh0IHRvIGRlY29kZQ"
        }
    }
  • Template:

    jinja
    Decoded String: {{ (coded_string + (4 - (coded_string | length % 4)) * '=') | b64decode }}
  • Output:

    Decoded String: Some text to decode

Other Python-Like Commands in Jinja

String Transformations

  • Convert to uppercase:
    jinja
    {{ my_string | upper }}
  • Convert to lowercase:
    jinja
    {{ my_string | lower }}

Mathematical Operations

  • Perform calculations:
    jinja
    {{ (value1 + value2) * 3 }}

Data Formatting

  • Format numbers:
    jinja
    {{ value | round(2) }}

Key Points

  • Python-like filters, such as b64decode, make it easy to manipulate data within Jinja templates.
  • You can combine basic math operations and string manipulations directly in Jinja to handle edge cases (e.g., incorrect Base64 padding).
  • These features provide flexibility and adaptability when working with dynamic workflows.