Skip to Content
0
May 26, 2021 at 04:44 PM

Groovy Script JSON to XML conversion

1017 Views

Dear Experts,

I am trying to generate XML file from JSON. Below is the code m using but I am getting blank as output.

Please help me to understand where I am doing mistake

Source

{
    "Message": {
        "EventType": "CandidateHired",
        "DepartmentId": 18956,
        "ProjectId": 143575,
        "CandidateId": 2109757
    },
    "DepartmentId": 18956,
    "CustomerId": 1709,
    "DateTimeUtc": "2018-10-10T08:32:13.6975051Z"
}

Code

import com.sap.gateway.ip.core.customdev.util.*;
import groovy.xml.*;
import groovy.json.*;

def Message processData(Message message)
{
//Body
    def body = message.getBody(String)
    def json = new JsonSlurper().parseText(body)
    def writer = new StringWriter()
    def builder = new MarkupBuilder(writer)

    builder {
        "Message" 
        {
            "EventType" json.Message.EventType
        }
    }

message.setBody(writer.toString())
}

TIA,

Rashmi