Error Invalid module output. Expected Object, but found Array

Hi, I’m trying to iterate through a response and output the item, but I seem to keep having the same issue. When running the scenario, it fails and gives the following error: error Invalid module output. Expected Object, but found Array .

I have found other people having the same issue and fixing it by tying to make an object out of it, but I can’t seem to get it to work.

In the code below, you can see my current attempt.

{
    "url": "/companies",
    "method": "GET",
    "qs": {
        "size": "{{ parameters.size }}",
        "page": "{{ parameters.pageNR - 1 }}"
    },
    "type": "urlencoded",
    "headers": {},
    "response": {
        "limit": "{{ body.page.totalPages }}",
        "output": "{{ item }}",
        "iterate": "{{ body.content }}"
    },
    "pagination": {
        "qs": {
            "page": "{{(pagination.page - 1)}}"
        }
    }
} 

When I tried fixing it by looking at other people’s examples, I had the following addition to the ouput:

"output": {
     "result": "{{ item }}"
},

But as mentioned before, I get the same response.

An example of the API response I get is as follows:

{
    "content": [
        {
            "id": "xxxx",
            "name": "xxxx",
            "address": {
                "street": "xxxx",
                "city": "xxxx",
                "postalCode": "xxxx",
                "country": "xxxx"
            },
            "website": "xxxx",
            "status": "xxxx",
            "billOnBehalfOfEnabled": xxxx,
            "orderApprovalRequired": xxxx
        },
        {
            "id": "xxxx",
            "name": "xxxx",
            "address": {
                "street": "xxxx",
                "city": "xxxx",
                "postalCode": "xxxx",
                "country": "xxxx"
            },
            "website": "xxxx",
            "status": "xxxx",
            "selfServiceAllowed": xxxx,
            "billOnBehalfOfEnabled": xxxx,
            "orderApprovalRequired": xxxx
        }
    ],
    "page": {
        "size": 2,
        "totalElements": 8,
        "totalPages": 4,
        "number": 0
    }
}

I saw such similar errors reported where the recommendation was to Create a Support ticket for further investigation.

However, please share the app name which is giving this error and a few screenshots of the scenario structure and the error.

I am not sure, but I may be able to help you with this.

1 Like

Hello,

I lost 3 hours on the same errors !

In your module change the type to Search, and it s’ok !

When you receive the "Invalid module output. Expected Object, but found Array." error, it means that the API returned a response type array (in layman’s terms, a list of items).

Since the current module type is set to action, the object type is expected (in layman’s terms, one item). Therefore, there are 2 solutions:

  1. Change the module’s type from action to search:
    Go to the module and in the upper right corner, click on Options, then select Edit Module. In the new window, select the option Search in the Type select.
    image

:bulb: It is also recommended to implement the pagination directive if the API supports it.

  1. Wrap the array in an object (not recommended):
 "response": {
            "output":
            {
                "myArray": "{{body}}"
            }
        }

The above solution is not a best practice since we want to iterate the items without the need to use the “Flow Controls > Iterator” module. Module type search, unlike the action type, supports pagination directives.

More information can be found here.