Over a million developers have joined DZone.

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.

· Integration Zone · Tutorial

Comment (1)

Save
33.82k Views
How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

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!

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.
Topics:
mule ,tutorial ,integration ,payload transformer ,json to java object

Comment (1)

Save
33.82k Views

Opinions expressed by DZone contributors are their own.

iOS Project: Integrating GitLab-CI, Fastlane, and HockeyApp

Follow the key steps to create a base framework for CI/CD process.

· Integration Zone · Tutorial

Comment (0)

Save
9,062 Views
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.

There are many ways to implement CI/CD for iOS projects and choosing the right combination of technology is all depends on your project type, existing infrastructure and customer requirements. Here we’re using Fastlane – an open source technology for HockeyApp uploading, but Fastlane itself is mature and capable of handling all tedious tasks. Here is the easiest way to setup CI/CD by using GitLab-CI, Runner, Fastlane and HockeyApp for final app distribution.

Integrating GitLab-CI, Fastlane, and HockeyApp

Please follow the below key steps to create a base framework for CI/CD process:

  • Access to existing GitLab account/Create a new one

  • Setup GitLab-Runner in MacOSX build machine

  • Register GitLab-Runner with GitLab-CI

  • Install Fastlane in build/development machine

  • Enable GitLab-CI and Fastlane

  • Access to existing HockeyApp account / Create a new one

Before starting CI/CD implementation, make sure that GitLab account is ready and the code has been pushed to the repository from your development machine.

Setting up GitLab-Runner (MacOSX Machine)

$ sudo curl --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-darwin-amd64
$ gitlab-runner install

Register GitLab-Runner Instance With Your GitLab Account

$ sudo gitlab-runner register

Before executing the above command please note down or copy gitlab-ci token and gitlab-ci coordinator url from GitLab account -> Settings -> CICD

Image title

Image title

Once you are done with gitlab-runner registration process, start the GitLab-Runner by executing the following command:

$gitlab-runner start

Please make sure the runner status in GitLab account and note that "tag name" can be used to identify the specific GitLab-Runner instance and which can be added in the .gitlab-ci.yml file.

Image title

Installing Fastlane in Build/Development Machine

You need to install Fastlane on both the build and development machine in order to test and configure.

Execute the following command from your build and development machine "Terminal" to install Fastlane.

$sudo gem install fastlane -NV

After installing Fastlane in development machine, go to project root directory and initialize Fastlane dependencies for Swift.

$fastlane init swift

Once initialization is completed, open "Fastfile.swift" in Xcode and insert the following code for uploading iOS binary to HockeyApp:

func appBetaDistributionToHockeyApp() {
//Fastlane API to upload signed ipa to HockeyApp
hockey(apiToken:<<Token from HockeyApp account>>, ipa: <</ios-builds/beta/hellogitlab.ipa>>, notes:<<any notes here>)
}

Enable GitLab-CI

To enable GitLab-CI, create a new file called ".gitlab-ci.yml" and save it in your project root directory. Insert the following xcodebuild commands to the yml file to clean, buildarchive and export ipa from GitLab-Runner. It is mandatory to create an export options plist file with proper code signing identity and details

-----------------

build_project:

script:

- "xcodebuild -workspace HelloGitLab.xcworkspace -scheme HelloGitLab clean"

- xcodebuild -workspace HelloGitLab.xcworkspace -scheme HelloGitLab -configuration Debug CODE_SIGNING_REQUIRED=“NO” CODE_SIGNING_ALLOWED=“NO” -destination generic/platform=iOS archive -archivePath /ios-builds/archive/path/HelloGitLab.xcarchive

- "xcodebuild -exportArchive -archivePath /ios-builds/archive/path/HelloGitLab.xcarchive -exportPath /ios-builds/dev/ipa/path -exportOptionsPlist /ios-builds/dev/exportoptions-development.plist”

- "fastlane appBetaDistributionToHockeyApp"

stage: build

tags:

- ios

stages:

- build

-----------------

Please validate your .gitlab-ci.yml file by using built-in GitLab validation tool or any yml lint tool available in online.

Export options plist

It is recommended to create multiple "export options" plist files for different targets like development, ad-hoc, enterprise etc with different code signing and provisioning profile details. Below is the sample one for development target.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>compileBitcode</key>
<false/>
<key>method</key>
<string>development</string>
<key>provisioningProfiles</key>
<dict>
<key>bundle identifier</key>
<string>Provisioning profile name</string>
</dict>
<key>signingCertificate</key>
<string>Signing certificate name</string>
<key>signingStyle</key>
<string>manual</string>
<key>stripSwiftSymbols</key>
<false/>
<key>teamID</key>
<string>teamID</string>
<key>thinning</key>
<string>&lt;none&gt;</string>
</dict>
</plist>

Once you complete all these steps, do a fresh commit and push the code into the GitLab repository and see the GitLab pipeline result.

Happy coding!

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:
ios,xcode,gitlab,cicd,swift,gitlab runner

Comment (0)

Save
9,062 Views

Opinions expressed by DZone contributors are their own.

9 Steps to Owning Your Code

9 Steps to Owning Your Code

Managing your code is something to be excited about. Establishing this accountability empowers developers with the information and control to ensure the services you build are production-ready and high-performing.