> For the complete documentation index, see [llms.txt](https://developers.make.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developers.make.com/custom-apps-documentation/app-components/iml-functions/removal-of-empty-collections-and-nulls.md).

# Removal of empty collections and nulls

There are API services that throw an error when you pass an empty collection or null for certain optional fields.

{% tabs %}
{% tab title="Request" %}

```json
{
    "name": "John Doe",
    "dob": "1970-01-01",
    ...
    "address": {}
}
```

{% hint style="info" %}
The collection `"address"` has empty data, which will cause an error response.
{% endhint %}
{% endtab %}

{% tab title="Error response" %}

> Validation Error
>
> Field "address" cannot be empty.
> {% endtab %}
> {% endtabs %}

{% tabs %}
{% tab title="Custom IML function: remove empty collections/arrays" %}

```javascript
function removeEmpty(input) {
    if (typeof input !== "object" || input === null) return undefined;
    return Object.entries(input).reduce((acc, [key, value]) => {
        if (Array.isArray(value)) {
            acc[key] = value.map(item => removeEmpty(item));
            return acc;
        }
        if (typeof value === "object" && value !== null && !(value instanceof Date)) {
            if (Object.keys(value).length === 0) return acc;
            acc[key] = removeEmpty(value);
            return acc;
        }
        if (value === null) return acc; // comment this line if you have to pass null values.
        acc[key] = value;
        return acc;
    }, {});
}
```

{% endtab %}
{% endtabs %}

In the module:

{% tabs %}
{% tab title="Variant 1" %}

```json
{
    "body": "{{removeEmpty(parameters)}}"
}
```

{% endtab %}

{% tab title="Variant 2" %}

```json
{
    "body": {
        "parameter1": "value1",
        "{{...}}": "{{removeEmpty(parameters)}}"
        }
}
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Correct request" %}

```json
{
    "name": "John Doe",
    "dob": "1970-01-01"
}
```

{% hint style="info" %}
The collection "address" was not entered in the request.
{% endhint %}
{% endtab %}

{% tab title="Successful response" %}

> 200 Success
> {% endtab %}
> {% endtabs %}


---

# 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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://developers.make.com/custom-apps-documentation/app-components/iml-functions/removal-of-empty-collections-and-nulls.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
