How to Handle Special Characters In Payload Data Using DataWeave
In this article I’ll show you how to handle the payload which contains special characters, similar error like below
Sample Input:
exp-1:- type - json
{
"message": "\$Hello world!"
}
exp-2:- type json
"Capital,Country \Delhi,IndiaParis,France"
exp-3:- type xml
<person><name>\John</name><age>25</age></person>
exp-4:-type csv
name,age
John,25
ERROR:
in this case how can we get the payload?
to handle the payload like above I implemented the function to handle those errors.
How to use the getData function in DataWeave
The answer is to use the getData function, which is a custom function that takes two parameters: inbound and mediaTypeValue. The function tries to read the inbound parameter as a data object using the mediaTypeValue parameter as the media type. If that fails, it tries to read the inbound parameter as a string. If both attempts fail, it returns null. The function also uses the result property to wrap the output in an object that indicates whether the operation was successful or not.
Here is the code for the function:
%dw 2.0
output application/json
import * from dw::Runtime
fun getData(inbound :String,mediaTypeValue :String) = (
(try(()-> read(inbound,mediaTypeValue))
orElseTry (() -> read(inbound))).result
)
---
(try(()->
if(typeOf(read(payload.^raw)) ~= "String") getData(payload.^,payload.^mediaType) else read(payload.^raw)
) orElseTry (() ->
getData(payload.^raw,payload.^mediaType)
)).result
OutPut for Input Data Mentioned Above:
The function uses the try-catch syntax to handle any errors that may occur while reading the inbound parameter. It also uses the orElseTry operator to chain multiple attempts until one succeeds or all fail. The result property is imported from the dw::Runtime module, which provides useful functions for working with DataWeave expressions.
To use the getData function, you need to pass it the input payload and its media type. However, depending on how the payload is structured, you may need to access different properties of the payload. For example, if the payload property that contains the actual raw data and a mediaType property that contains the media type, you can use the following expression:
getData(payload.^raw,payload.^mediaType)
However, if the payload.^ does not have a result property, but instead has a raw property that contains the data as a string and a mediaType property that contains the media type.
To handle both cases, you can use a conditional expression that checks the type of the payload using the typeOf function. The typeOf function returns the value that indicates whether the type check was successful or not. You can use this property to determine which expression to use for calling the getData function. For example:
if(typeOf(read(payload.^raw)) ~= "String") getData(payload.^,payload.^mediaType) else read(payload.^raw)
This expression checks if the payload type is String or not. If yes, it calls the getData function with payload.^,payload and payload.^mediaType as arguments. If no, it calls the read function with payload.^raw as arguments.
However, there is another possibility: what if the payload parseing error out? In that case, you may want to return null or throw an error. To handle this case, you can use another try-catch syntax to wrap the conditional expression and catch any errors that may occur. For example:
(try(()->
if(typeOf(read(payload.^raw)) ~= "String") getData(payload.^,payload.^mediaType) else read(payload.^raw)
) orElseTry (() ->
getData(payload.^raw,payload.^mediaType)
)).result
This expression tries to execute the conditional expression and returns its result. If any error occurs, it returns null instead. You can also replace null with any other value or expression that you want to return in case of an error.
To summarize, the getData function is a useful way to read and parse input payloads in DataWeave regardless of their structure and media type. You can use it with different expressions depending on how your payload is structured and handle any errors gracefully using try-catch syntax and result property.