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

  • Proper Java Exception Handling
  • Generics in Java and Their Implementation
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)
  • JSON-Based Serialized LOB Pattern

Trending

  • Cookies Revisited: A Networking Solution for Third-Party Cookies
  • Evolution of Cloud Services for MCP/A2A Protocols in AI Agents
  • Measuring the Impact of AI on Software Engineering Productivity
  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era
  1. DZone
  2. Coding
  3. Languages
  4. Payload Transformation: JSON to Object

Payload Transformation: JSON to Object

See how to transform an incoming JSON payload into a Java object. Lots of development work revolves around payload transformation, so this is important to know.

By 
Christopher Co user avatar
Christopher Co
·
Apr. 16, 17 · Tutorial
Likes (8)
Comment
Save
Tweet
Share
97.5K Views

Join the DZone community and get the full member experience.

Join For Free

When you are first starting out with Mule, it is very important to learn the basics, such as how to manipulate the incoming payload.

In this article, I am going to explain how you to transform an incoming payload (JSON) to a Java object.

Most of the work in development revolves around payload transformation, so it is very important to tackle this — especially for beginners.

I will first show the basics, and later on, we will get into the more advanced stuff.

The Basics

Let's first start by posting a very basic JSON string as payload.

Image title

Given the JSON string above, let's now define a transformer component in our flow.

In our case, we are going to use a JSON to Object transformer.

Image title

Under the General properties of the JSON to Object transformer component, you can define your desired return class.

Let's define java.util.HashMap as the return class for this example.

Image title

Once you run the flow above, our JSON string payload is then converted to a Java object HashMap. You will then be able to access the properties of your payload through payload.<propertyname>, wherepropertyname is defined in the JSON string (i.e. payload.age).

You can also use other Java Objects such as java.util.ArrayList, java.util.LinkedList, etc.

Taking It Further

What if you would like to transform the incoming JSON string into a custom Java object?

In order for us to satisfy this, let's first define a custom Java class (Student) withGET and SET methods.

Image title

Note: The custom Java class' variables should have the exact same name (case sensitive) as the JSON string's properties. If the names are different, the transformer component will throw an error.

Once this is done, we can then change the return class of the JSON to Object transformer component we defined earlier to our custom class: packageName.className.

For example, myschool.model.Student:

Image title

Let's try to debug this example.

Image title

As you can see, our original payload has been transformed into the custom Java object Student.

We can then try to access the properties of our payload through payload.<propertyname>,propertyname being defined in the custom Java class — i.e. payload.age.

Next Steps

How about transforming the JSON String into a custom Java object with a custom Java object children?

Does this also work using the JSON to Object component?

The answer is yes!

Let's try to make our JSON string a little bit more complex.

Image title

We now have an additional JSON properties named subjects.

If we are going to use the previous custom Java class we defined, the transformer component will throw an UnrecognizedPropertyException exception since we have not defined any property named subjects in our Java class.

In order for us to satisfy this condition, let's first start creating another class named Subject and add the list of Subject into our Student class later on.

Our Subject class should look like this:

Image title

After defining our Subject class, we can then add it into our Student class.

Add the following code into Student.java: private List subjects; .

Don't forget to add the GET and SET methods for the newly added variable subjects.

Image title

Let's now test our application and check to see if the payload transformation to our newly defined custom Java class is successful.

Image title

As you can see from the image above, our JSON string has been successfully transformed into our new custom Java object!

JSON Object (computer science) Payload (computing) Java (programming language) Strings Data Types

Opinions expressed by DZone contributors are their own.

Related

  • Proper Java Exception Handling
  • Generics in Java and Their Implementation
  • Commonly Occurring Errors in Microsoft Graph Integrations and How To Troubleshoot Them (Part 4)
  • JSON-Based Serialized LOB Pattern

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!