Skip to main content
Graham Snow a posé une question dans #MuleSoft

Receiving an invalid Json payload, but still need to handle it, or generate notifications to the customer? This might be the thread for you.  

 

I recently had to solve this problem and a colleague thought it was worth sharing my solution. I hope this is useful!

 

The Challenge

 

  1.  You're expecting a valid Json payload, but don't know for sure it will be valid every time
  2. The payload may contain control characters which aren't considered valid in UTF-8, or diacritic characters not supported in your process. For example "Schönblist" or "Burichstra\u001fe" causing Mule to throw an error: 
Failed parsing field: content org.mule.runtime.core.api.expression.ExpressionRuntimeException: "Unexpected character '\u001f' at payload@[1:479] (line:column), expected '"', while reading `payload` as Json.

 

The Solution 

 

  1. Change the http:listener to receive a MIME-type of text/plain instead of application/json
  2. Add a transform step to set a variable isValidJson by validating if the payload (text/plain) can safely be converted to application/json:

%dw 2.0

output application/java

import * from dw::Runtime

var parsed = try(() -> read(payload, "application/json"))

---

parsed.success

 

  3. Add a choice block. When isValidJson == true then add a Transform step to convert the payload to application/json and continue your flow as expected. 

 

%dw 2.0

output application/json

---

read(payload, "application/json")

 

4. When isValidJson == false, extract any useful data in order to generate an alert or notification as required. As the payload is not valid Json, the easiest way to do this is with a regular expression in a Transform step.  In this case employeenumber is the name of a specific attribute. The value we are interested in appears after the attribute name: 

 

%dw 2.0

import * from dw::core::Strings

output application/java

---

(replaceAll((payload as String),"\"","") scan /(?:employeenumber\s*:\s*)(\d+)/)[0][1]

 

Here's another example of extracting some useful information with a regular expression. This one identifies the specific fields in the payload which contain the invalid characters: 

 

%dw 2.0

output application/json

var fieldsWithInvalidChars = payload scan /(\w+)"\s*:\s*"\w+([\u0000-\u001F\u007F]|[^\u0000-\u007F])/

var errorDetail = (fieldsWithInvalidChars map (i) -> i[1] ++ " contains \"" ++ i[2]) joinBy "\" and "

---

{

"employee": {

"employeeNumber": vars.employeeNumber,

"firstname": null,

"surname": null

},

"errorMessage": p('new-employee.error-message.invalid-character') ++ errorDetail ++ "\"",

"errorStatusCode" : "422",

"subscriber" : p('new-employee.queue.subscriber')

}

 

Final Tips 

 

  1. If using a regular expression, I'd recommend going for the scan function, not the match function. scan supports a wider range of regular expression symbols. For example, positive lookahead "?:" is supported in scan, but not in match.
  2. When writing your regular expression, use a site like pythex.org, or regexr.com to validate the regex works as expected.
  3. Consider also looking at https://docs.mulesoft.com/json-module/latest/json-schema-validation

 

#MuleSoft

1 réponse