icon/x Created with Sketch.

Splunk Cookie Policy

We use our own and third-party cookies to provide you with a great online experience. We also use these cookies to improve our products and services, support our marketing campaigns, and advertise to you on our website and other websites. Some cookies may continue to collect information after you have left our website. Learn more (including how to update your settings) here.
Accept Cookie Policy

We are working on something new...

A Fresh New Splunkbase
We are designing a New Splunkbase to improve search and discoverability of apps. Check out our new and improved features like Categories and Collections. New Splunkbase is currently in preview mode, as it is under active development. We welcome you to navigate New Splunkbase and give us feedback.

Accept License Agreements

This app is provided by a third party and your right to use the app is in accordance with the license provided by that third-party licensor. Splunk is not responsible for any third-party apps and does not provide any warranty or support. If you have any questions, complaints or claims with respect to this app, please contact the licensor directly.

Thank You

Downloading jinja_formatter
SHA256 checksum (jinja_formatter_105.tgz) 88c802ff23d1f22bd64481855865e597e9c17bf1758e7da885cbcc8296118f5c SHA256 checksum (jinja_formatter_104.tgz) a5cbb0fb40f76055f71a1274548dc5b7a6f5392a78a02f1a4ccd27d7cf84bae4 SHA256 checksum (jinja_formatter_103.tgz) 17d0b79ea0883129c408053843bfd3a55201cd6ddb4a7458cbd3443b00712101 SHA256 checksum (jinja_formatter_102.tgz) cc2369e11d2552acd9bf148d83b3d8be9d17c18044b63cfc9ba63212eebedeca SHA256 checksum (jinja_formatter_101.tgz) 5528222dfd5d84109537637b8693ac46dda7e1f4abe31d6337a9c1433fa3df0f
To install your download
For instructions specific to your download, click the Details tab after closing this window.

Flag As Inappropriate

splunk

jinja_formatter

Splunk Cloud
Overview
Details
In many cases, people format a user-readable content within Splunk -- either because they need to send an e-mail, show a text in a dashboard, or push the results to JIRA or other ticketing tool.

But when they format the actual readable message, they have to use a very simple formatting techniques, making the query look rather ugly and difficult to maintain.

With a template, it's possible to specify the resulting text with placeholders, which will later on be properly populated by the query results. And that's exactly what `jinja2format` does.

Description

The jinja2format command returns events with a one new field,
formatted_template, unless you specify the result option.

Example

The following example will output the rendered template into
formatted_template field:

  | makeresults count=1 
  | eval celsius = random()%100 
  | eval name = "Joe" 
  | jinja2format "It's {{ celsius }} degrees, {{ name }}!"

In the following example, we override the output field, and use a Splunk
variable to hold the template.

  | makeresults count=1
  | eval celsius = random()%100 
  | eval name = "Joe" 
  | eval template="It's {{ celsius }} degrees, {{ name }}!"
  | jinja2format result=out template

This is generally better if the template is complex, and/or
contains characters that could break passing the value to
the jinja2format Splunk app -- mostly quotes, perhaps
something else too, but I am not really sure what exactly.

Custom Filters and Jinja Functions

Over time, we realized we need more functions to be able to efficiently
implement our templates.

In order to do so, we've added the following custom filters & functions.

Custom Filters

toyaml(value: object)

Take the given object and try to render it as YAML structure, with
some default pretty-printing.

fromjson(value: str)

Take a string and turn it into a JSON object. This can be useful in a combination
with tojson() function, which nicely formats the given JSON object.

{ value | fromjson | tojson(2) }

will take the value and, if everything is well, it'll format it
to a nice JSON representation.

tolist(value: object)

Convert the given value to a list. This is probably only useful for multi-value
Splunk fields that may or may not be multivalued. By default, a multi-value field
is passed as a list, but single-value field is a string. With

strftime(unix_timestamp: str, format_string: str = "%Y-%m-%dT%H:%M:%S%z")

Convert the given unix_timestamp to a human-readable timestamp. By default,
it uses ISO 8601 standard, but you can provide your own format_string.

Useful for many Splunk searches, because timestamps are usually in the Epoch
format, not in the human-readable one.

b64decode(value: str)

Take the given string and apply base64 decoding to it.

b64encode(value: str)

Take the given string and apply base64 encoding to it.

Custom Functions

zip(list1, list2, ...)

Allows you to aggregate elements from multiple iterables/list into a single
list. It takes two or more lists as input and returns an iterator that
produces tuples containing elements from all the input iterables.

In many cases, you may want to use list() filter as the follow-up filter in
order to convert the output to a list.

zip test: {{ zip(ip, domain, mv) | list }}

zip_longest(list1, list2, ..., fillvalue=None)

This function makes an iterator that aggregates elements from each of the
iterables. The iteration continues until the longest iterable is not exhausted.

It takes two or more lists as input and returns an iterator that
produces tuples containing elements from all the input iterables.

Complex Example

The following example shows probably all the functionality
implemented in the current release of jinja2format command.

    | makeresults count=1 
    | eval celsius = random()%100 
    | eval mvtest=mvappend("value1", "value2")
    | eval mv = mvappend("value1", "value2", "value3", "value4", "value5")
    | eval ip=mvappend("ip1", "ip2", "ip3")
    | eval domain=mvappend("domain1", "domain2", "domain3", "domain4")
    | eval encoded="w5pwbG7EmyDFvmx1xaVvdcSNa8O9IGvFr8WI"
    | eval mvtest="value1"
    | eval name = "Joe" 
    | eval tj = "{\"dict\": { \"key1\": \"1234-5678-90ab\", \"key2\": \"abcdef\"}}"

    | eval template="
    It's {{ celsius }} degrees, {{ name }}! It's year {{ _time | strftime('%Y') }} now. 

    How about a YAML test? 
    ```yaml
    {{ tj | fromjson | toyaml }}
    ```

    How about a JSON test?
    ```json
    {{ tj | fromjson | tojson(2) }}
    ```

    Dealing with occasional multivalues: {{ mvtest | tolist }}

    zip test: {{ zip(ip, domain, mv) | list }}
    zip_longest test: {{ zip_longest(ip, domain, mv) | list }}

    Test of the `zip_longest` in a loop:
    {%- for (iip, idomain, imv) in zip_longest(ip, domain, mv, fillvalue='-') %}
      - IP: {{ iip }}; domain: {{ idomain }}, mv: {{ imv }}
    {%- endfor %}

    Test of the YAML functions:
    {{ zip(ip, domain, mv) | list | toyaml }}

    Test b64decode:
      - {{ encoded | b64decode }}
    " 
    | jinja2format result=out template

Release Notes

Version 1.0.5
April 5, 2025

Potential RCE fix -- use jinja2 sandboxed environment.
Update dependencies to their latest versions.

Version 1.0.4
Nov. 27, 2024

Add more useful features (filters and custom commands) to the jinja2format command:

  • strftime
  • zip and zip_longest
  • fromjson and toyaml
  • b64encode and b64decode

Along with extra documentation.

Version 1.0.3
Oct. 30, 2024

splunk-sdk upgraded to the latest available version, plus a few compliance updates to the app configuration

Version 1.0.2
March 22, 2023

Less restrictive permissions, as it might have been causing problems in some installations.

Version 1.0.1
March 16, 2023

Subscribe Share

Are you a developer?

As a Splunkbase app developer, you will have access to all Splunk development resources and receive a 10GB license to build an app that will help solve use cases for customers all over the world. Splunkbase has 1000+ apps from Splunk, our partners and our community. Find an app for most any data source and user need, or simply create your own with help from our developer portal.

Follow Us:
Splunk, Splunk>,Turn Data Into Doing, Data-to-Everything, and D2E are trademarks or registered trademarks of Splunk LLC in the United States and other countries. All other brand names,product names,or trademarks belong to their respective owners.