Hi Experts,
I'm trying to build a custom json to xml conversion via groovy. Reason for this is have just one single step in the iflow rather many steps with low functional changes. I have below sample input:
{
"INPUT": {
"h1": "v1",
"h2": "v2",
"ITEMS": {
"item": [
{
"i1": "t1",
"i2": "t2",
"i3": "t3"
},
{
"i1": "t4",
"i2": "t5",
"i3": "t6"
},
{
"i1": "t7",
"i2": "t8",
"i3": "t9"
}
]
}
}
}
Expected output is:
<INPUT> <h1>v1</h1> <h2>v2</h2> <ITEMS> <item> <i1>t1</i1> <i2>t2</i2> <i3>t3</i3> </item> <item> <i1>t4</i1> <i2>t5</i2> <i3>t6</i3> </item> <item> <i1>t7</i1> <i2>t8</i2> <i3>t9</i3> </item> </ITEMS> </INPUT>
However, I got below as result:
<INPUT> <h1>v1</h1> <h2>v2</h2> <ITEMS> <item> <i1>[t1, t4, t7]</i1> <i2>[t2, t5, t8]</i2> <i3>[t3, t6, t9]</i3> </item> <item> <i1>[t1, t4, t7]</i1> <i2>[t2, t5, t8]</i2> <i3>[t3, t6, t9]</i3> </item> <item> <i1>[t1, t4, t7]</i1> <i2>[t2, t5, t8]</i2> <i3>[t3, t6, t9]</i3> </item> </ITEMS> </INPUT>
I have below code for now. Please advise what I am missing/misunderstanding.
import com.sap.gateway.ip.core.customdev.util.Message
import groovy.json.*
import groovy.xml.*
def Message processData(Message message)
{
def body = message.getBody(String)
def json = new JsonSlurper().parseText(body)
def writer = new StringWriter()
def builder = new MarkupBuilder(writer)
builder.INPUT {
h1(json.INPUT.h1)
h2(json.INPUT.h2)
ITEMS {
json.INPUT.ITEMS.item.each{
item {
i1(json.INPUT.ITEMS.item.i1)
i2(json.INPUT.ITEMS.item.i2)
i3(json.INPUT.ITEMS.item.i3)
}
}
}
}
message.setBody(writer.toString())
return message
}