cancel
Showing results for 
Search instead for 
Did you mean: 

Convert payload to JSON in SAP CPI

former_member538292
Participant
0 Kudos
899

Hi All,

I am getting the data from source system into CPI camelpath header like (date='20190203',supplier='1234567890',company='ABCD')

now i need to convert this into xml.i tried to csv to xml no luck.I thought to convert to json using groovy script easy.

I just replace the = -> :,( ->{ but unable add the double " " and replace single to double quotes.

Could you please help me.

EX:(date='20190203',supplier='1234567890',company='ABCD')

I want to like <date>20190203</date> <supplier>123456789</supplier> <company>abcd</company>

Like below also help and i will use json to xml conversion

{"data":"20190203","supplier":"123456789","company":"ABCD"}


Thanks a lot.

Kumar

Accepted Solutions (1)

Accepted Solutions (1)

PriyankaChak
Active Contributor
0 Kudos

Hi Kumar,

Please try with below code.

path is the header name. You have to put Camel header name over there.

Code

import com.sap.gateway.ip.core.customdev.util.Message

import groovy.xml.*

def Message processData(Message message)

{

def data = message.getHeaders().get("path")

def date_val = (data =~ /date=\'(.*?)\'[,]/)[0][1]

def supplier_val = (data =~ /supplier=\'(.*?)\'[,]/)[0][1]

def company_val = (data =~ /company=\'(.*?)\'[)]/)[0][1]

def writer = new StringWriter()

def xml = new MarkupBuilder(writer)

xml.root(){

date(date_val)

supplier(supplier_val)

company(company_val)

}

message.setBody(writer.toString())

return message

}

Regards,

Priyanka

Answers (1)

Answers (1)

former_member538292
Participant
0 Kudos

Hi Priyanka,

It is working.

Thanks a lot,

Priyanka