cancel
Showing results for 
Search instead for 
Did you mean: 

Remove Element in JSON after conversion

markbernabe
Active Participant
0 Kudos

Hi Experts,

I have the following XML:

<?xml version="1.0" encoding="UTF-8" ?> <root> <element> <name>Donald</name> <gender>M</gender> <country>US</country> <id>1</id> </element> <element> <name>Hilary</name> <gender>F</gender> <country>FR</country> <id>2</id> </element> <element> <name>Obama</name> <gender>M</gender> <country>JP</country> <id>3</id> </element> </root>

After XML-JSON Converter, it will become:

{"element": [{"name":"Donald","gender":"M","country":"US","id":"1"}, {"name":"Hilary","gender":"F","country":"FR","id":"2"}, {"name":"Obama","gender":"M","country":"JP","id":"3"}] }

Problem is, my desired JSON message is

[{"name":"Donald","gender":"M","country":"US","id":"1"}, {"name":"Hilary","gender":"F","country":"FR","id":"2"}, {"name":"Obama","gender":"M","country":"JP","id":"3"} ]

Obviously, I don't want the "element" in my JSON message. So my question is, is it possible to omit this "element"?

Thanks in advance!

Accepted Solutions (1)

Accepted Solutions (1)

MortenWittrock
Active Contributor

Hi Mark

There's only so much you can do in the XML to JSON converter, so if you need to craft more specific JSON output, you need to do so in code. Try adding the following Groovy script after the converter step:

import com.sap.gateway.ip.core.customdev.util.Message;
import groovy.json.JsonSlurper
import groovy.json.JsonOutput

def Message processData(Message message) {
    def body = message.getBody(java.lang.String) as String
    def jsonParser = new JsonSlurper()
    def jsonObject = jsonParser.parseText(body)
    message.setBody(JsonOutput.toJson(jsonObject["element"]))
    return message;
}

This assumes you've checked "Suppress JSON Root Element".

Regards,

Morten

markbernabe
Active Participant

Thanks Morten! Was also able to do it via JavaScript

importClass(com.sap.gateway.ip.core.customdev.util.Message);
importClass(java.util.HashMap);
 
function processData(message) {
	var data = JSON.parse(message.getBody(java.lang.String));
	var request = JSON.stringify(data.element);
	message.setBody(request);
	return message;
}
MortenWittrock
Active Contributor
0 Kudos

Yeah, JavaScript might even be the more natural choice 🙂

0 Kudos

hi guys,

do you know how to convert xml into json array?

I need - "specializations": [{ "id": "12.180" }, { "id": "24.493" }],

if i use xsd schema

<xs:element name="specializations" minOccurs="0" maxOccurs="unbounded">

<xs:complexType>

<xs:sequence>

<xs:element type="xs:float" minOccurs="0" maxOccurs="unbounded" name="id"/>

</xs:sequence>

</xs:complexType>

</xs:element>

i only get "specializations":{"id":["12.180","24.493"]}, that's not execlty what reciever wants.

0 Kudos

Hi Alexey,

Please check the answer from my colleague Mandy and also from Mark Bernabe below.

Regards,

Gayathri

former_member302452
Participant
0 Kudos

Hi Morten,

When i am using this groovy script its working fine but all fields are shuffling.

import com.sap.gateway.ip.core.customdev.util.Message;import groovy.json.JsonSlurper
import groovy.json.JsonOutput

def Message processData(Messagemessage){
    def body =message.getBody(java.lang.String)asString
    def jsonParser =new JsonSlurper()
    def jsonObject = jsonParser.parseText(body)message.setBody(JsonOutput.toJson(jsonObject["MT_Request"]))returnmessage;}

Is there any other script.

Please find the below link.

https://answers.sap.com/questions/364055/how-to-remove-root-element-in-json-after-conversio.html

Thanks and Regards,

Md.Thouheed

Answers (2)

Answers (2)

markbernabe
Active Participant

Hi Alexey,

I believe the closest XSD that you can use is this

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="root">
<xs:complexType>
<xs:sequence>
<xs:element name="specializations">
<xs:complexType>
<xs:sequence>
<xs:element name="element" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="id" type="xs:float"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema> 

Then use XML to JSON converter with Suppress JSON Root element enabled. The result will be:

{"specializations":{"element":[{"id":"12.180"},{"id":"24.493"}]}} 

Then one option is to add a script like below to get rid of "element' and have your desired output

function processData(message) {
var new_array = {
specializations: []
}

var data = JSON.parse(message.getBody(java.lang.String));
new_array.specializations = data.specializations.element;
message.setBody(JSON.stringify(new_array));

return message;
}

Hope this helps.

0 Kudos

Hi everyone thanks for answers,

i found out that it can be done in standard conversion step.

You need to point elements to be an array under Convert XML Element to JSON Array

mandy_krimmel
Advisor
Advisor

Hello,

successive elements with the same name are always transformed to an array in the JSON converter, therefore you get an array value for the id element.

This is also described in the documentation of the JSON converter.

Best regards,

Mandy