> For the complete documentation index, see [llms.txt](https://docs.locoia.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.locoia.com/automation/flow-builder/jinja2-template-language/magic-code-samples.md).

# Magic Code Samples

Jinja code examples for tricky things.

### Email parsing

How to get [`first.last@email.com`](mailto:first.last@email.com) the **first** and **last** name in two variables in Jinja?

```django
{% set first, last = email.split("@")[0].split(".") %} 
{{ first }} {{ last }}
```

How to get the myname of [`myname@email.com`](mailto:myname@email.com) before the @ nicely, e.g. to use as a first name?

```
{% set first = email.split("@")[0] %}
{{ first }}
```

###

### Mapping to lists

If you have two **lists**, one of which can be **nested**, and you want to combine items of it, do as follows:

List 1 called `list_1`:

```json
{
    "items": [
        {
            "taxCode": "23456",
            "salesTaxRate": {
                "taxRate": "0.0001"
            }
        }
    ]
}
```

List 2 called `list_2`:

```json
{
    "id": "11",
    "custcol_tax_code": "23456",
    "amount": 1000.00,
    ...
}
```

In the [Dict Helper > Define variable](/connectors/helpers/dict-helper.md):

```django
...

  {% for line_item in list_1["items"] %}
    {% for item in list_2 %}
      {% if line_item.taxCode == item.custcol_tax_code  %}
      
          "line_item_id": "{{ item.item.id }}"
          "amount": "{{ item.amount }}"
          "tax_rate": "{{ line_item.salesTaxRate.taxRate }}"

      {% endif %}
    {% endfor %}
  {% endfor %}

...
```

{% hint style="danger" %}
Since **items** is reserved in Python, and we use Python in the backend, you need to call it like **`list_1["items"]`**`, rather than standard dot notation.` \
\
`list_1.items is NOT possible.`
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.locoia.com/automation/flow-builder/jinja2-template-language/magic-code-samples.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
