0

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?

Volodymyr
  • 17
  • 3

1 Answers1

0

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)}}"
}
Midimix
  • 281
  • 1
  • 2
  • 3