Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Use Validate JSON Schema and Message Enricher in Mule

DZone's Guide to

How to Use Validate JSON Schema and Message Enricher in Mule

When working with data flows in a MuleSoft application you may need to work with Validate JSON Schema and Message Enricher functions. Learn how!

· 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.

Hi all! Today, in this tutorial, we are going to learn how to use the Validate JSON Schema and Message Enricher. I have considered a small scenario to demonstrate the Validate JSON Schema and Message Enricher as shown below.

Complete Flow:

Image title

In this scenario, we are sending a JSON Payload as input and, first, we validate the JSON payload using the Validate JSON Schema. And if the Schema is valid then we insert the payload into a database table and the same payload should be written to a file directory. Since we all know that after the database, the payload gets changed, in order to pass the same payload to the target file directory, we use Message Enricher.

  • First, drag and drop the HTTP Listener connector from the Mule palette to the Message Flow.
  • Configure the HTTP Listener with the default configuration, i.e. Host: localhost and Port: 8081.
  • Now drag and drop a Validate JSON Schema from the Mule Palette onto the Flow.
  • Validate JSON Schema:  According to the Mule docs, "the JSON Schema validator evaluates JSON payloads at runtime and verifies that they match a referenced JSON schema. You can match against schemas that exist in a local file or in an external URI."

  • Now go to src/main/resources and create a JSON Schema for Validation, here I have created the file, InputSchema.json.
  • Please Find the InputSchema.json that I have used in this tutorial in the below snippet:

    {
    
       "title": "Product",
       "type": "object",
    
       "properties": {
    
          "EmpID": {
             "description": "The unique identifier for an Employee",
             "type": "integer"
          },
    
          "Name": {
             "description": "Name of the Employee",
             "type": "string"
          },
    
          "Designation": {
             "description": "Designation of the Employee",
             "type": "string"
          },
    
          "Experience": {
       "description": "Designation of the Employee",
             "type": "integer"
            }
       },
    
       "required": ["EmpID", "Name", "Designation", "Experience"]
    }
    • Now enter the file name in which you have saved the JSON Schema at the Schema Location while configuring the Validate JSON Schema as shown below:Image title
    • If the Input Schema from the HTTP Listener is valid, then drag and drop a Message Enricher from the Mule Palette on to the Flow.

    Message Enricher: According to the Mule docs, "One common scenario involves the need to enrich an incoming message with information that isn’t provided by the source system. You can use a content enricher if the target system needs more information than the source system can provide."Image title

    • Now drag and drop the Database Connector from the Palette under the Message Enricher.
    • Configure the Database as per your configuration with host, port, user, and password.
    • Then open Message Enricher's properties and enter #[payload] in the Source, and, for the Target, we can create a variable like session/record/flow variables. But here I have configured it as #[sessionVars.database] as shown below:Image title
    • Now drag and drop the File Connector and configure it with the target directory where the file should be written along with the file name as I have chosen to be a date time, i.e.                         #[server.dateTime.format("yyyyMMdd'T'HHmmss").toString()].json where it writes the files with a JSON extension.
    • Last I have used a Set Payload Component to display the Success Message response for the HTTP Listener.
    • Once you run the application, pass the input JSON payload as shown below:Image title

    The Input JSON Payload is given in the below snippet:

    {
          "EmpID": 143,
          "Name": "Venkat Rachapudi",
          "Designation": "Programmer Analyst",
          "Experience": 2
       }
    • You can see the details that were entered into the database as below:Image title

    • Finally, the same payload is successfully written to the target file directory using Message Enricher as shown below:Image titleBy now, you should have an understanding of how to use the Validate JSON Schema and Message Enricher.

    Hope this post helps you. I'm always ready to accept feedback.

    Thank you!

    Download A Buyer's Guide to Application and Data Integration, your one-stop-shop for research, checklists, and explanations for an application and data integration solution.

    Topics:
    message enricher ,json schema ,anypoint connectors ,integration ,mulesoft

    Opinions expressed by DZone contributors are their own.

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

    {{ parent.tldr }}

    {{ parent.urlSource.name }}