top of page

JSON Module Component in Mule 4

GitHub repository with the Mule Project can be found at the end of the post.



The JSON Module Connector is used to validate a JSON Payload against the predefined JSON Schema of your choice. Since schema validation falls outside the scope of DataWeave Functionality, the JSON Module can be used.



To use the JSON module, add it manually to your Mule app in Anypoint Studio or Design Center; or, if you want to use Maven, you can add the following dependency into your pom.xml file:


<dependency>
	<groupId>org.mule.modules</groupId>
	<artifactId>mule-json-module</artifactId>
	<version>RELEASE</version>
	<classifier>mule-plugin</classifier>
</dependency>

For better understanding of how the JSON Module works, I did a proof of Concept. Below is the screenshot of the flow which includes the JSON Module component.


validatejson flow scheduler read logger validate json schema on error propagate type json schema not honoured write logger

To validate the JSON payload, I need an example JSON file (in my case, document.json) and I would need the JSON schema file (mySchema.json) to validate my example JSON file against it. Since I chose to read the document.json file, I placed it below src/main/resources/examples:


src main resources application types log4j2 api examples document json schemas myschema json

The document.json file looks like this:


document json employeeid employeename

The schema against which the document.json file would be validated is defined as below:


myschema json schema type array items type object properties employeeid type string minlength 4 maxlength 6 employeename type string minlength 1 maxlength 255 required employeeid employeename

A fixed frequency scheduler is set to run the flow every few seconds/minutes. The document.json file is read using the File Read operation as shown below:


read display name read basic settings connector configuration file general file path examples document json

Once the file is read, the payload is saved in the Target Variable jsonDoc as shown below:


read advanced lock false time between size check 2 time between size check until milliseconds streaming strategy repeatable file store stream in memory size 512 buffer unit kb output target variable jsondoc target value payload

After the file is saved in the jsonDoc variable, it is validated against the schema using the JSON Module as shown below:


validatejson flow validatejson module scheduler read logger validate schema error handling on error propagate json schema not honoured write logger validate schema display name general schema schemas myschema json content vars jsondoc schema redirects none dereferencing canonical

As per our example, document.json does not match mySchema.json, so, it throws a JSON:SCHEMA_NOT_HONOURED exception. This exception is caught in the error handler and the error.errorMessage.payload is written to a file as shown below:


error handling on error propagate type json schema not honoured write general basic settings connector configuration file config general path jsonschemas json content error errormessage payload create parent directories true write mode append

The file will contain the following error message payload:



The JSON Module can throw any of these exceptions:

  • JSON:INVALID_INPUT_JSON

  • JSON:INVALID_SCHEMA

  • JSON:SCHEMA_NOT_FOUND

  • JSON:SCHEMA_NOT_HONOURED


Some JSON schemas might reference other schemas through a public URI. However, you might not want to fetch those schemas from the internet, mainly for performance and security reasons.


The JSON Module has an option to redirect the schema, which lets you replace an external reference with a local one, without the need to modify the schema itself.


The JSON Module also has an option called "Allow Duplicate Keys", which when set true, it will allow the duplication of keys; otherwise, it will fail.



This is why the JSON Module is a very useful component for schema validation. Enjoy the benefits of using this component :)


Thank you for reading.

-Pravallika Nagaraja



GitHub repository




2,045 views3 comments
bottom of page