Over a million developers have joined DZone.

Validate JSON Schema Components in Mule

DZone's Guide to

Validate JSON Schema Components in Mule

Learn how to use the JSON Schema validator to validate payloads against a schema in a Mule application, with code included.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

The JSON Schema validator evaluates JSON payloads at runtime and verifies that they match a referenced JSON schema. We can match against schemas that exist in a local file or in an external URI.

If the payload is not compliant with JSON schema, then we will get this Exception: org.mule.module.json.validation.JsonSchemaValidationException: Json content is not compliant with schema.

Use Case

This feature is especially useful when we are consuming a service that expects JSON inputs and that must match a specific schema. Only the JSON payload which is successfully validated against the schema will pass through otherwise JSON schema validation exception will be raised.


<json:validate-schema schemaLocation="EmployeeSchema.json" doc:name="Validate Json Schema" />

Let’s walk through how to use the JSON Schema validator in a Mule application. Using the JSON Schema Validator is very simple: we only need to provide a reference to the schema file you want to validate against on schemaLocation. The Schema Validator accepts both local and external resources. In this example, we have "EmployeeSchema.json" located in the src/main/resources folder. We are expecting the JSON payload as part of the request which will be validated against the schema.

Mule Flow:

Image title


<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:json="http://www.mulesoft.org/schema/mule/json"
xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.8.1"
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd 
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">

<http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" basePath="/json" doc:name="HTTP Listener Configuration" />
    <flow name="myFirstSchemaValidator">  
        <http:listener config-ref="HTTP_Listener_Configuration" doc:name="HTTP" path="/json"/>
        <json:validate-schema schemaLocation="EmployeeSchema.json" doc:name="Validate Json Schema" />
       <logger message="Schema Validated successfully" level="INFO"/>

Now, to execute this flow, we will have to open the REST client and pass the payload as part of the POST request body. Below are the success and failed requests for the flow.

Success Request:

    "firstName": "Ankit",
    "lastName": "Lawaniya",
    "age": 28

Image title

Failed Request:

    "firstName": "Ankit",
    "age": 28

Image title

Hope this helps.


Keep learning.

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

mule ,mulesoft ,mule esb ,json

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}