DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Validate XML Request Against XML Schema in Mule 4
  • Pydantic: Simplifying Data Validation in Python
  • Exploring JSON Schema for Form Validation in Web Components
  • Datafaker Gen: Leveraging BigQuery Sink on Google Cloud Platform

Trending

  • Java Virtual Threads and Scaling
  • Evolution of Cloud Services for MCP/A2A Protocols in AI Agents
  • A Complete Guide to Modern AI Developer Tools
  • A Guide to Developing Large Language Models Part 1: Pretraining
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Validate JSON Request Against JSON Schema in Mule 4

Validate JSON Request Against JSON Schema in Mule 4

In this post, we will learn about the JSON schema and the steps involved in validating JSON requests against a JSON schema in Mule 4.

By 
Ashish Jha user avatar
Ashish Jha
·
Aug. 22, 23 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
12.5K Views

Join the DZone community and get the full member experience.

Join For Free

JSON (JavaScript Object Notation) is a  type of data format that is widely used in modern technology to interchange data. Validating a JSON schema ensures that the data complies with a predefined structure or schema, which is essential for ensuring data integrity and compatibility between different systems.

Before We Start, Let's Understand What JSON Schema Is

JSON schema is a specification that is written in JSON format. It defines a set of keywords that allow you to express various constraints and formats for JSON data. 

These keywords are used to specify the allowed data types, minimum and maximum values, patterns for string values, and other rules. It serves as a blueprint that defines the expected properties, data types, and constraints for JSON documents. 

JSON schema is particularly useful for ensuring data consistency, compatibility, and data quality in systems that exchange JSON data.

Use Case: JSON Schema Validation

JSON Payload

JSON
 
{
	"Status": true,
	"message": "Response from demo-validation-sapi app",
	"number": 1234
}


For this tutorial, we will pass the above payload with a POST request to the /json endpoint and validate it using a JSON schema. 

I have created a demo project with /json endpoint and /xml endpoint for JSON and XML validation, respectively. In this tutorial, we will use the /json endpoint.

For JSON validation, we will use "json-validation-flow" with /json endpoint. Before configuring the validation module, if we make a call to /json endpoint with the above JSON payload, it gives a 200 status code.

Steps To Validate the Payload Against JSON Schema

Step 1: Add the JSON module in the studio from Anypoint Exchange.

Add the JSON module in the studio from Anypoint Exchange.

Verify whether the JSON module has been added or not.

Validate Schema

Step 2: Prepare the JSON schema. For creating the JSON schema, you can use any free online JSON to JSON schema generator tool available. (For this tutorial, I have used - liquid-technology.)

In the schema generator, add the payload and generate the schema.

Add the payload and generate the schema

Generated Schema for the above JSON payload:

JSON
 
{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "Status": {
      "type": "boolean"
    },
    "message": {
      "type": "string"
    },
    "number": {
      "type": "integer"
    }
  },
  "required": [
    "Status",
    "message",
    "number"
  ]
}


Step 3: In the project's "src/main/resources" folder, create a folder with the name "schemas" (or any name you prefer). Create a file with the name "json-schema.json" inside the "schemas" folder.

Note: Make sure that the file type of json schema is ".json".

Step 4: Copy and paste the generated JSON schema to the "json-schema.json" file.

Copy and paste the generated JSON schema to the "json-schema.json" file.

Step 5: Add the JSON "Validate schema" processor before the "Transform Message" (or any other processor which needs the validated JSON data). 

Add the JSON "Validate schema" processor before the "Transform Message" (or any other processor which needs the validated JSON data).

Configure the JSON schema path in the "Validate schema" processor.

Configure the JSON schema path in the "Validate schema" processor.

Step 6: In the "Error handling" section, add an "On Error Propagate" scope and set the type to "JSON:SCHEMA_NOT_HONOURED" error type.

 In the "Error handling" section, add an "On Error Propagate" scope

Step 7: Add the "httpStatus" variable and error message in the error handler. (In this tutorial, I have set the "httpStatus" to 400 for error response).


 Add the "httpStatus" variable and error message in the error handler. (In this tutorial, I have set the "httpStatus" to 400 for error response).

Step 8: Set the "httpStatus" variable in HTTP Listener as "Status code" in the "Responses" section.

Set the "httpStatus" variable in HTTP Listener as "Status code" in the "Responses" section.

Step 9: Deploy the project and send a valid request to the/json endpoint. We should get a successful response with a status code of 200.

Deploy the project and send a valid request to the/json endpoint.

Now make some changes to the request payload. We should get an error response with a status code of 400. (Here, I changed the "message" field to "messages").

We should get an error response with a status code of 400.

Step 10: At this point, if we pass any extra fields in the JSON request body, it will allow the extra field to pass.
At this point, if we pass any extra fields in the JSON request body, it will allow the extra field to pass.

Note: In the above screenshot, the "id" field is extra, we did not define the "id" field in the JSON schema, but still, the request was successful.

If we want to restrict the extra fields that are not defined in the schema, we have to add "additionalProperties": false" to the schema.


If we want to restrict the extra fields that are not defined in the schema.

Step 11: Save the schema and redeploy the app. Now make a call to the /json endpoint with extra fields in the request payload. It should return an error message with 400 as the status code.

 Save the schema and redeploy the app.

This way, we can validate a JSON request payload against a JSON schema.

For more information about the Mule JSON module, refer to the MuleSoft documentation here.

You can get the source code of the above application here.

I hope you find this tutorial helpful.

Error message JSON MULE Use case Schema Data validation

Opinions expressed by DZone contributors are their own.

Related

  • Validate XML Request Against XML Schema in Mule 4
  • Pydantic: Simplifying Data Validation in Python
  • Exploring JSON Schema for Form Validation in Web Components
  • Datafaker Gen: Leveraging BigQuery Sink on Google Cloud Platform

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!