Custom IML function in body

Hello :slight_smile:

I try to figure if there is a way to use custom IML function in body.

{
	// Request to API endpoint.
	"url": "/CUSTOMERS", // Relative to base URL
	"method": "POST",
	"headers": { }, // Additional HTTP headers
	"qs": {  }, // Query string
	"body": "{{arrayToObject({{parameters.labels}})}}",
	"response": {
		"error": {
			"type": "RuntimeError",
			"message": "[{{statusCode}}] {{body.error.message}}",
			"400": {
				"type": "DataError",
				"message": "[{{statusCode}}] {{body.error.message}}"
			},
			"500": {
				"type": "ConnectionError",
				"message": "[{{statusCode}}] {{body.error.message}}"
			}
		},
		"output": "{{body}}"
	}
}

I’m getting errors
the IML function:

function arrayToObject(arr) {
  return arr.reduce((obj, item) => {
    obj[item.column] = item.value;
    return obj;
  }, {});
}

I need to transform this:

[
    {
        "value": "3",
        "column": "CUSTNAME"
    },
    {
        "value": "2",
        "column": "STATDES"
    },
    {
        "value": "1",
        "column": "NSFLAG"
    }
]

to this:

{
    "CUSTNAME": "3",
    "STATDES": "2",
    "NSFLAG": "1"
}

In my environment its works,

any ideas guys? :slight_smile:

Thank you in advance,
Leon

Hi Leon,

It seems that you wrote too many braces.
Since you didn’t deliver any error messages I just guess you should reduce

{{arrayToObject({{parameters.labels}})}}

to

{{arrayToObject(parameters.labels)}}

Let me know if it worked.
Cheers

2 Likes