# Dict Helper

<figure><img src="https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2FtObyOTnGeenKf5Kpm8aa%2Fimage.png?alt=media&#x26;token=ee210e42-3730-4fb2-8e16-10fb2cb80289" alt="" width="188"><figcaption></figcaption></figure>

## Overview&#x20;

Dict Helper is not only one of the most popular tools, but it is also a powerful tool for Flow. It is capable of storing values, filtering them, parsing [Jinja expressions](#3.-jinja-expressions), and much more.

## What is essential for you to understand

[Dictionaries](#1.-dictionaries-dict), essentially JSON objects adapted for efficient program communication, along with [lists](#2.-lists), constitute the most commonly employed methods for transferring data between programs and bridging the gap between browsers and servers.

### **1. Dictionaries (Dict)**

A *dictionary* is a structure that is essentially a list of key: value pairs.&#x20;

{% code title="Example" %}

```python
{
  "name": "John Doe",
  "age": 23,
  "address": {
    "street": "123 Main Street, Boston",
  }
}
```

{% endcode %}

[More info](https://www.w3schools.com/python/python_dictionaries.asp)

### **2. Lists**

A list, similar to an array, is a versatile data structure designed for storing collections of items. Lists can accommodate a wide range of data types, including:

* Numbers (both floating-point and integers)
* Strings
* Nested lists
* Dictionaries

This flexibility makes lists a fundamental tool for managing and organizing various types of data."

{% code title="Example" %}

```python
[
  {"name": "John", "age": 23},
  {"name": "Bob", "age": 25},
  {"name": "Charlie", "age": 35}
]
```

{% endcode %}

[More info](https://www.w3schools.com/python/python_lists.asp)

### **3. Jinja expressions**

Jinja is a powerful templating engine designed for Python. It seamlessly integrates with Dict Helper, allowing you to effortlessly execute logic by passing Jinja expressions directly. \
You can learn more [here](https://docs.locoia.com/automation/flow-builder/jinja2-template-language)

## Actions

### **1.** Define variables for later use

This action has only one input that accepts dictionaries, lists, and Jinja expressions.\
It allows you to reference data from Previous Steps, File Uploads, and Environment Variables within your workflow, enabling you to execute custom logic.\
In this example, the variable `contacts` comes from a previous step:

{% code title="Example" %}

```python
{% if contacts | length < 100 %}
  "this is fine"
{% else %}
  "too much"
{% endif %}
```

{% endcode %}

### **2. Filter List V2**

The enhanced version of the Filter makes it effortless to apply filtering in a no/low-code manner. Filtering is carried out using a variety of predefined filter operators tailored to different data types. \
Some of them:

* Exists
* Equal to
* Greater than
* Less than
* Contains
* Is empty

<figure><img src="https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2FumcvEpoRtDHf2HK9sDWL%2Fimage.png?alt=media&#x26;token=f992d887-02e9-4460-95b9-c75ace4be57c" alt=""><figcaption></figcaption></figure>

### **3.** Remove list from lists (`flatten list`)

* Nested lists
* Dictionaries

For instance: if you have a list like&#x20;

{% code title="Example of list of lists" %}

```python
[
  [1,2,3],
  [4,5,6],
  [7,8,9]
] 
```

{% endcode %}

you get `[1, 2, 3, 4, 5, 6, 7, 8, 9]` back.

### **4.** Store Value

Add any kind of value to a dictionary

<figure><img src="https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2FIxARUZVSs8i11D2y4nn8%2Fimage.png?alt=media&#x26;token=593c712c-cbc6-4f30-9ca5-b1763c94869e" alt=""><figcaption></figcaption></figure>

### **5.** Add string to the dictionary

This feature enables you to append a new field with a specified value to an existing dictionary. For instance, if you have a Zendesk user object containing `first_name` and `last_name`, you can easily include the `age` by specifying both the **Field name** and the desired **Content**:

<figure><img src="https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2FzXEBOAgep0KHFUltkkvI%2Fimage.png?alt=media&#x26;token=ea2db313-a19c-4232-bf6e-83efffa768a8" alt=""><figcaption></figcaption></figure>

### 6. Add string to list&#x20;

It allows you to add a new string to an existing list.&#x20;

<figure><img src="https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2F31jWpjv2TQVImzk2Nhso%2Fimage.png?alt=media&#x26;token=3d7eadc0-b1eb-48c4-91df-eaf28ba16e64" alt=""><figcaption></figcaption></figure>

### **7. Replacing fields in lists or dictionaries**

With this action, you can replace a value in the specified place.

For example, you have a user (zend1.user):

```json
{
   "user":{
      "first_name": "Max",
      "last_name": "Miller",
      "email": "max.miller@mac.com"
   }
}
```

And you want to replace the value of the field `first_name` in the JSON / dictionary response of Zendesk with `my_new_name`:

![Replacing fields in dictionaries](https://s3.amazonaws.com/cdn.freshdesk.com/data/helpdesk/attachments/production/48013396418/original/uZEVn78Vjd1Vomdc84obQNwlRxw5sQ_iYQ.png?1572302762)

After replacing:

```json
{ 
  "user": {
    "first_name": "my_new_name",
    "last_name": "Miller",
    "email": "max.miller@mac.com"
    }
}
```

**Replace a value in the list:** Similarly to the previous example you need to specify the destination - index to access `n` element in the list. So to replace the user's email here you need to enter it's index \[2]<br>

<pre class="language-javascript"><code class="lang-javascript">[
<strong>  "Max",
</strong>  "Miller",
  "max.miller@mac.com"
]
</code></pre>

<figure><img src="https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2FfSJiXOgBchG6WI39khsn%2Fimage.png?alt=media&#x26;token=24c45e99-1708-4e06-96f6-3c098770873c" alt=""><figcaption></figcaption></figure>

### **8. Update value**

Update any kind of value in a dictionary

<figure><img src="https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2FC2kJjBvAPqQWRtzOL1OM%2Fimage.png?alt=media&#x26;token=e452f5e5-6188-4ae4-a74a-31a2cd733bfe" alt=""><figcaption></figcaption></figure>

### **9. Append Value**

This is very similar to [#6.-add-string-to-list](#6.-add-string-to-list "mention")but more powerful\
It allows you to add an entire row or object to a list.&#x20;

<figure><img src="https://291121471-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-McrRFZHYH27bqKzOVDd%2Fuploads%2Fq9adVqNBl82DP0rlkYCy%2Fimage.png?alt=media&#x26;token=f6dfb2a6-e9ac-471e-a966-5db7c4852543" alt=""><figcaption></figcaption></figure>

### **10.** Combine Lists

This helps you to unite two lists in one

Example:

{% code title="list\_1" %}

```python
[
  "Barcelona",
  "Berlin"
]
```

{% endcode %}

{% code title="list\_2" %}

```python
[
  "Porto",
  "Kyiv"
]
```

{% endcode %}

will be united into

{% code title="list\_combined" %}

```python
[
  "Barcelona",
  "Berlin"
  "Porto",
  "Kyiv"
]
```

{% endcode %}

### **11.** Convert List Of Dicts To List Of Lists

Converts **list of dicts** (JSON objects):

{% code title=" list of dicts" %}

```json
[
   {
      "email": "miller@gmail.com",
      "last_name": "Miller",
      "first_name": "Max"
   },
   {
      "email": "peter@mac.com",
      "last_name": "Zoal",
      "first_name": "Peter"
   }
]
```

{% endcode %}

to **list of lists:**

{% code title="list of lists" %}

```python
[
  [
    "miller@gmail.com",
    "Miller",
    "Max"
  ],
  [
    "peter@mac.com",
    "Zoal",
    "Petr"
  ]
]
```

{% endcode %}

### **12.** Filtering

{% hint style="info" %}
For a large list, larger than **50k** entries or **5 MB**, please use the [Spreadsheet helper with action Query spreadsheet](https://docs.locoia.com/connectors/spreadsheet-helper#query-spreadsheet), as it can better handle large data.
{% endhint %}

{% hint style="info" %}
You can also use the Jinja filter `selectattr` to filter without having to use an action.

[More details can be found here.](https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.selectattr)
{% endhint %}

#### **Filtering a list**&#x20;

will most likely be used a lot for CSV files or e.g. GoogleSheet content that is a list of lists. Please see the section here on [filtering CSVs here](https://docs.locoia.com/connectors/csv-helper#filtering-a-csv-file-for-certain-rows-records-only).

#### Filtering a JSON / dictionary (dict) <a href="#filtering-a-json-dictionary-dict" id="filtering-a-json-dictionary-dict"></a>

As an example, let's say you want to filter a list of Freshdesk or Zendesk tickets. The response JSON of an API would look like this:

```javascript
{
   "tickets": [
      {ticket1 ... some more JSON},
      {ticket2 ... some more JSON},
      {ticket3 ... some more JSON}
   ...
   ]
}
```

The corresponding connector setup looks like this:

![](https://s3.amazonaws.com/cdn.freshdesk.com/data/helpdesk/attachments/production/48006740157/original/owyaxZbcERF_RePpkeYGfUzwHvyO5TcNbA.png?1568039203)

As a list reference in this case, you would provide **zen1.tickets** in order to only get the list of tickets, because as you can see, within the above JSON object is again a list, indicated by the squared brackets \[ ]. So essentially, you could say the above is a JSON object that wraps a list of ticket objects, which are again JSON.

If you want to filter for a particular data record e.g. after pulling all your Zendesk tickets, you want to get all tickets where the `requester_id` is `12345`, you could filter for:

```
item.requester_id == 12345
```

An alternative notation which would yield the same result would be HTML

```
12345 in item.tags
```

More examples:

```
"email" in item.profile and item.deleted == False and item.updated >= (unix_timestamp - 3* 24 * 60 * 60)
```

```
item.created_date > "{{ convert_datetime(date, '%Y-%m-%d', -30) }}" and item.status == "active" and item.email[-10:] != "locoia.com"
```

{% hint style="info" %}
Note that you cannot use the `item` parameter in combination with Jinja, here need you to use **Python** instead.
{% endhint %}
