Empty collection sends data in the POST request

In a module, I have the following Mappable Parameters:

{
    "name": "name",
    "type": "text",
    "label": "Name",
    "help": "Contact name"
},
{
    "name": "company",
    "type": "collection",
    "label": "Company",
    "spec": [
        {
            "name": "name",
            "type": "text",
            "label": "Company name"
        },
        {
            "name": "address",
            "spec": [
                {
                    "name": "street",
                    "type": "text",
                    "label": "Street"
                },
                {
                    "name": "number",
                    "type": "number",
                    "label": "Number"
                },
                {
                    "name": "city",
                    "type": "text",
                    "label": "City"
                },
                {
                    "name": "country",
                    "type": "text",
                    "label": "Country"
                }
            ],
            "type": "collection",
            "label": "Company Address",
            "advanced": true
        }
    ]
}

However, when the user does not provide any company data, the JSON looks like this:

[
    {
        "name": "Example Name",
        "company": {
            "address": {}
        }
    }
]

And in my API, since company has a value (which in this case is address), it does not work. How can I fix this?

What is the final shape of the data you want?

If no “company” field is filled in, I don’t want it to appear in the output JSON, resulting in:

{
    "name": "Name example"
}

Welcome to the Make community!

In your custom app’s Connection, you can modify the payload before it’s sent. You can try using an ifempty function to set the field to “omit” or “undefined”, or if+length functions if it’s a complex variable.

For more information, see Processing of input parameters | Make Developer Hub

Hope this helps! Let me know if there are any further questions or issues.

— @samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

1 Like

I use this occasionally when trying to filter empty objects:

function filterEmptyObjects(data) {
    const result = {};
    for (const key in data) {
        if (Object.prototype.hasOwnProperty.call(data, key) && !(typeof data[key] === 'object' && Object.keys(data[key]).length === 0)) {
            result[key] = data[key];
        }
    }
    return result;
}

Pass the entire payload in:

“body”: “{{filterEmptyObjects(parameters)}}”,

Send Make a support ticket to add custom IML functions to your app.

1 Like