Technology Blog Posts by Members
cancel
Showing results for 
Search instead for 
Did you mean: 
mounikanamineni
Explorer
2,446

Hey Readers, It's been a long time since I posted a blog again! But believe me I just came up with an interesting topic. I am sure at one or the other point it will be useful! 😀

Do you know that we can map the data and convert it to json format without using palette options?
 🤔 Obviously yes!

In this blog lets see about how to map the data and convert back to json format without using the message mapping and converters in integration flow.

Of course, I am not very good at coding 😅 but yes! I follow some simple tricks to make our work easy in our day to day life.

Objective:
Map the incoming data to target structure and convert back to JSON format using Groovy Script.

Source Structure:

{
    "id": "456783421908765z",
    "time": "2025-01-13T08:25:37.066663700Z",
    "data":
    {
        "moduleName": "Create",
        "firstName": "SAP",
        "lastName": "CPI",
        "startDate": "02/16/2025",
        "endDate": "03/31/2025",
        "code": "1234"
    }
}

Target Structure:

{

       "uniqueID": "456783421908765z" ,
        "module": "Create",
        "fName": "SAP",
        "lName": "CPI",
        "startDate": "02/16/2025",
        "endDate": "03/31/2025"
}

Integration Flow Details:
Step1: Lets build a simple integration flow by adding a groovy script.
step1.png

Step2: For capturing the incoming payload lets create a property in the content modifier. If the value is true then capture the payload else not.
step2.png

Step3:
Ah! Our most important code in this blog... Lets add some pieces of code to do mapping in groovy without using palette options.

def Message processData(Message message)
{
def messageLog = messageLogFactory.getMessageLog(message);
def body = message.getBody(java.lang.String) as String;

//get properties
def log = message.getProperty("log") as String;

//log input payload
if(log == "true")
{
if(messageLog != null)
{
messageLog.addAttachmentAsString("Input Payload", body.toString(), "text/plain");
}
}

//extract data from the json
Map jsonMsg = new JsonSlurper().parseText(body);

def id = jsonMsg.id;
def moduleName = jsonMsg.data.moduleName;
def firstName = jsonMsg.data.firstName;
def lastName = jsonMsg.data.lastName;
def sDate = jsonMsg.data.startDate;
def eDate = jsonMsg.data.endDate;

//construct Target payload
def jbuilder = new JsonBuilder();
def jslurpur = new JsonSlurper();

jbuilder
{
uniquelD id
module moduleName
fname firstName
lname lastName
startDate sDate
endDate eDate
}

output_payload = jslurpur.parseText(jbuilder.toString());
message.setBody(JsonOutput.toJson(output_payload));
return message;

}

Execution:

Finally, Its time to execute! I have executed the code  by using groovy IDE.

Tadaaa! The Output is here! 🤗

step3.png

You can also see the input payload attached on the bottom right side of the page under
'Attachments'.


Okay then, I hope this blog is informative! Let's catch up on the next blog.


please feel free to drop a comment and don't forget to follow me on linkedin
Mounika - Linkedin 

Thanks & Regards,
Mounika.