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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

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

Trending

  • Go 1.24+ Native FIPS Support for Easier Compliance
  • SaaS in an Enterprise - An Implementation Roadmap
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 2
  • Caching 101: Theory, Algorithms, Tools, and Best Practices
  1. DZone
  2. Coding
  3. Languages
  4. Mule 4: JSON Schema Validation

Mule 4: JSON Schema Validation

Let's take a look at a JSON schema validation as well how to match against defined schemas that exist in a local file or in an external URI.

By 
Murali Thuraka user avatar
Murali Thuraka
·
Updated Aug. 20, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
36.7K Views

Join the DZone community and get the full member experience.

Join For Free

JSON Schema is a specification for a JSON-based format for defining the structure of JSON data. It validates input data at runtime and verifies that they match a referenced schema or not. We can match against defined schemas that exist in a local file or in an external URI.

If the payload is incorrect with given JSON schema, then compiler throws the below exception:

org.mule.module.json.validation.JsonSchemaValidationException: JSON content is not compliant with schema.

Use Case

Validating the input JSON payload against with JSON Schema.

JSON Payload:

{
  "firstName": "Murali",
  "lastName": "Krishna",
  "age" : 26
}

JSON Schema:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    }
  },
  "required": [
    "firstName",
    "lastName",
    "age"
  ]
}

Mule Flow

Step 1:

Configure the HTTP Listener with by giving hostname, port number and path along with this specify allowed methods (Optional) at an Advanced tab of HTTP connector.

Step 2:

Drag and Drop the JSON Validate Schema from Mule Palette to validate the input payload. And provide the schema path. In my case it is like below:

schemas/Sample-Schema.json

From the above line,

schemas ----> It is a directory

Sample-Schema.json ----> It is JSON-Schema structure for validation.

Syntax of JSON Validator as below:

<json:validate-schema doc:name="Validate schema" doc:id="5a8b10e1-59e8-4f68-9aaa-303c9cb5c9d6" schema="schemas/Sample-Schema.json"> 

Step 3:

Drag and drop the Logger component to log the resultant payload after the validation.

Final Config.xml:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:validation="http://www.mulesoft.org/schema/mule/validation"
xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/validation http://www.mulesoft.org/schema/mule/validation/current/mule-validation.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" doc:id="8a601d72-5913-4ed7-99d3-707601301ec9" >
<http:listener-connection host="0.0.0.0" port="8080" />
</http:listener-config>
<flow name="abcFlow" doc:id="30917fd1-0429-4ec7-9d7d-aa8d4d19413e" >
<http:listener doc:name="Listener" doc:id="2b89fed0-69ce-47eb-93bf-3bd0628fe188" config-ref="HTTP_Listener_config" path="abc" allowedMethods="POST">
<ee:repeatable-file-store-stream />
</http:listener>
<json:validate-schema doc:name="Validate schema" doc:id="5a8b10e1-59e8-4f68-9aaa-303c9cb5c9d6" schema="schemas/Sample-Schema.json">
</json:validate-schema>
<logger level="INFO" doc:name="Logger" doc:id="26b62866-2f25-4374-9d95-9fe14c052366" message="Payload is Validated ----&gt; #[message.payload]" />
</flow>
</mule>

Success Scenario:

Image title

Failure Scenario:

Image title

Thank you!

Please feel free to share your thoughts in the comments section.

JSON Schema

Opinions expressed by DZone contributors are their own.

Related

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

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!