This document discusses validating JSON payloads in Mule applications. It explains that JSON is a lightweight data format that is easy for humans to read and write. The document also provides an example of a valid JSON payload structure. It then demonstrates a Mule flow that uses a json:is-json-filter component to validate incoming JSON payloads against a file inbound endpoint. If validation fails, it routes the message to a sub-flow that logs a rejection message. This allows validating JSON payloads and handling invalid ones gracefully.
5. Before we start we will look what is a JSON and how JSON
payload looks like.
JSON (JavaScript Object Notation) is a lightweight data-
interchange format. It is easy for humans to read and write.
JSON is a text format that is completely language independent
but uses conventions that are familiar to programmers of the
C-family of languages, including C, C++, C#, Java, JavaScript,
Perl, Python, and many others. These properties make JSON an
ideal data-interchange language.
Source :- http://www.json.org/
7. We can use a Mule component called json:is-json-filter in our
flow to validate a JSON payload
8. So, we can take following Mule flow to demonstrate :-
As you can see there is a flow and a sub flow. A message filter is there after
the file inbound filter which wrap a json:is-json-filter in it to validate the
JSON payload. We will use file inbound endpoint in our flow to pick file
containing JSON payloads
9. The subflow is used for throwing a message in case the payload is an invalid
JSON content
10. The corresponding Mule flow will be :-
<file:connector name="File_Global" autoDelete="true"
outputAppend="true" streaming="true" validateConnections="true"
doc:name="File" />
<sub-flow name="ValidationFailFlow" doc:name="ValidationFailFlow">
<logger message="Please provide valid JSON Content" level="INFO"
doc:name="Logger" />
<set-payload
value="Message has been rejected by filter ... Please provide valid JSON Content"
doc:name="Set Payload" />
</sub-flow>
<flow name="JSONValidationFlow" doc:name="restConsumeFlow">
<file:inbound-endpoint path="E:backuptest"
responseTimeout="10000" doc:name="File" connector-ref="File_Global">
<file:filename-regex-filter pattern="complex.json"
caseSensitive="false" />
</file:inbound-endpoint>
<message-filter onUnaccepted="ValidationFailFlow"
doc:name="Message">
<json:is-json-filter />
</message-filter>
<logger message="Valid JSON" level="INFO" doc:name="Logger" />
</flow>
11. So, let us consider we use the following JSON as a payload and
keep it as a file named complex.json in E:backuptest
location-
{
"id" : 1,
"name" : "Anirban",
"age" : 29,
"email" : "anirban1234@gmail.com",
"Designation": "SSE"
}
12. Since it is a valid JSON content we will get the following in our
console
13. Now, if we test our application we with an invalid JSON like :-
{
"id" : 1,
"name" : "Anirban",
"age" : 29
"email" : "anirban1234@gmail.com",
"Designation": "SSE"
}
You can see the above JSON is invalid as I deliberately deleted
the comma between the attributes to make it as invalid. Now
we save this JSON in our complex.json in E:backuptest so
that file inbound endpoint picks it up.
14. Now our Mule application throws the following message in
console :
Now when we execute this flow, we will get the above in our console