cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

JSON to XML Converter Fails due to bad field name

beverely_parks2
Participant
0 Kudos
2,002

I am receiving a JSON token that is not using standard file names. Instead of "issued": I'm receiving ".issued": and ".expires":. The standard JSON to XML converter was working but it starting erroring over the weekend. I've tried to use a groovy script prior to the standard conversion to rename ".issued" to "issued" and ".expires" to "expires", but when it runs it is dropping the double quote" in front of the tag for "expires_in". This is resulting in the error: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 278 path $.token_type. The script that I'm using is:

def msgBodyOriginal = message.getBody(java.lang.String) as String;

def msgBodyModified = msgBodyOriginal.replaceAll(".issued","issued");

def msgBodyModified2 = msgBodyModified.replaceAll(".expires", "expires");

message.setBody(msgBodyModified2);

return message;

}

I've tried just replacing all occurrances of "." but it results in an empty body. Any and all help is appreciated!!

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
SAP Mentor
SAP Mentor

Hi Beverely

As apalagin points out (and you should accept their answer), this is because of the updated behaviour of the JSON to XML converter. .issued and .expires cannot be converted to XML element names (since they must begin with a letter or a number), and therefore the conversion fails.

What you need to do, is either process the JSON in a script, or use a script to update the offending names before the conversion. Your script is almost correct. The first parameter of the replaceAll method is a regular expression, and in a regular expression, the dot translates to any character whatsoever. If you quote it, your script will work. So something like this:

def body = message.getBody(String)
def newBody = body.replaceAll("\\.issued", "issued").replaceAll("\\.expires", "expires")
message.setBody(newBody)
return message

Regards,

Morten

beverely_parks2
Participant
0 Kudos

Thank you, Morten! That is all that I needed to do. It's working correctly now.

Answers (2)

Answers (2)

AlexeyP
Participant

Hi Beverly

Potentially, this could be because of the CPI upgrade which included changes to the JSON to XML converter, see details here.

Cheers

Alexey

beverely_parks2
Participant
0 Kudos

Are there any options other than asking SAP to reverse this enhancement?

sbapanapalli65
Explorer
0 Kudos
Hello Experts,
sbapanapalli65
Explorer
0 Kudos
Hello Experts, I am trying to fix JSON elements which are preventing conversion of JSON to XML using code "import com.sap.gateway.ip.core.customdev.util.Message; def Message processData(Message message) { // Get the body as a String (assuming it's already a String) def body = message.getBody(String) // Perform replacements def newBody = body.replaceAll("Ignore Volume?", "IgnoreVolume") // Set the modified body back to the message message.setBody(newBody) return message }" . I don't see the results. Payload remains unchanged. What is that I am missing. Thanks in Advance. Suresh