javascriptmake.comintegromat-apps

Get root object of a dynamic server response


I am implementing an API that returns different root objects inside the body. For example:

{
   "money_expected_revenue": {
   // payload
   }
}
or
{
   "money_other_revenue": {
   // payload
   }
}

The module output should be the payload inside the root object. How can this be done?


Solution

  • You'll need to write a custom IML function, use the Javascript method Object.entries() to retrieve the first key-value pair, and return the value:

    function getRootObj(body) {
        if (body && Object.entries(body).length === 1)
            return Object.entries(body)[0][1];
        return {};
    }
    

    Then, in the module's Communication tab, use this function in the output:

    "response": {
        "output": "{{getRootObj(body)}}"
    }