I am working in Mule 4 using DataWeave 2.0 and trying to transform a multipart/form-data payload that contains a CSV file.
Here is a simplified example of my input payload (multipart):
----------------------------974030822407525684705359
Content-Disposition: form-data; name="alert_name"
kle-process-api-netiq-hr
----------------------------974030822407525684705359
Content-Disposition: form-data; name="incident_description"
Please find attached the errors generated during the data sync from Workday to NetIQ \n\nCorrelation ID - 3243444-0e5b-4fbd-817a-d28365a3414a\nTransaction ID - dsdsdsddd-d09c-4be3-a676-e45c487ca106\nSystem Name - pro-netiq-hr\nTotal number of error messages - 2
----------------------------974030822407525684705359
Content-Disposition: form-data; name="file"; filename="error.csv"
Content-Type: text/csv
correlationId,applicationName,errorProviderName,messageObjectName,messageObjectKey,enumber,vnumber,createdDateTime,errorResponseStatusCode,errorResponsePayload
67ghg34f-7584-4439-8e62-d10d22070186,kle-process-api-netiq-hr,NetIQ API,worker-object,987654,E54321,V097654,2025-08-13T12:05:23,500,"{\""id\"": \""ID-test-90bd8d7c-fb64-4b5c-9585-fa8e94b19dc4\"",\""result\"": \""ERROR\"",\""code\"": \""500\"",\""application\"": \""kle-system-api-netiq-hr\"",\""provider\"": \""NetIQ API\"",\""payload\"": \""Internal Server Error : An internal error occurred. Please try again. : <html>\\r\\n<head><title>502 Bad Gateway</title></head>\\r\\n<body>\\r\\n<center><h1>502 Bad Gateway</h1></center>\\r\\n</body>\\r\\n</html>\\r\\n\""}"
----------------------------974030822407525684705359--
Expected output:
----------------------------974030822407525684705359
Content-Disposition: form-data; name="alert_name"
kle-process-api-netiq-hr
----------------------------974030822407525684705359
Content-Disposition: form-data; name="incident_description"
Please find attached the errors generated during the data sync from Workday to NetIQ \n\nCorrelation ID - 3243444-0e5b-4fbd-817a-d28365a3414a\nTransaction ID - dsdsdsddd-d09c-4be3-a676-e45c487ca106\nSystem Name - pro-netiq-hr\nTotal number of error messages - 2
----------------------------974030822407525684705359
Content-Disposition: form-data; name="file"; filename="error.csv"
Content-Type: text/csv
correlationId,applicationName,errorProviderName,messageObjectName,messageObjectKey,enumber,vnumber,createdDateTime,errorResponseStatusCode,errorResponsePayload
67ghg34f-7584-4439-8e62-d10d22070186,kle-process-api-netiq-hr,NetIQ API,worker-object,987654,E54321,V097654,2025-08-13T12:05:23,500,"{id: ID-test-90bd8d7c-fb64-4b5c-9585-fa8e94b19dc4,result: ERROR,code: 500,application: kle-system-api-netiq-hr,provider: NetIQ API,payload: Internal Server Error : An internal error occurred. Please try again. : <html>\\r\\n<head><title>502 Bad Gateway</title></head>\\r\\n<body>\\r\\n<center><h1>502 Bad Gateway</h1></center>\\r\\n</body>\\r\\n</html>\\r\\n}"
----------------------------974030822407525684705359--
Dataweave I tried
%dw 2.0
output application/csv header=true
---
payload.parts.file.content map ((item, index) -> {
correlationId: item.correlationId,
applicationName: item.applicationName,
errorProviderName: item.errorProviderName,
messageObjectName: item.messageObjectName,
messageObjectKey: item.messageObjectKey,
enumber: item.enumber,
vnumber: item.vnumber,
createdDateTime: item.createdDateTime,
errorResponseStatusCode: item.errorResponseStatusCode,
errorResponsePayload: do {
var payloadStr = item.errorResponsePayload default ""
var extractedMessage =
if (payloadStr contains "\"payload\"")
do {
var parsedJson = read(payloadStr, \application/json)
var payloadContent = parsedJson.payload default payloadStr
var cleanedContent = payloadContent
replace /<[^>]*>/ with ""
replace /\\r\\n/ with " "
var normalizedContent = cleanedContent replace /\s+/ with " "
---
trim(normalizedContent)
}
else payloadStr
---
extractedMessage
}
})
Problem
When I test this in the DataWeave Playground, I get this error:
Unable to resolve reference of: application
Unable to resolve reference of: json
I have tried multiple variations (like escaping the content type, or using single quotes), but I am still not getting the desired transformation.
Question
What is the correct way in DataWeave to:
- Read and parse the JSON string inside a CSV field?
- Clean up HTML and newline characters?
Any help or example script would be appreciated.