Get root object of a dynamic server response

173 Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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)}}"
}