/scripts/ahub.form.attachments.js
0

Remove Element in JSON after conversion

Jan 20, 2017 at 03:52 PM

668

avatar image

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!

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Morten Wittrock
Jan 21, 2017 at 07:13 PM
1

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

Show 5 Share
10 |10000 characters needed characters left characters exceeded

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;
}
3

Yeah, JavaScript might even be the more natural choice :)

0
Former Member

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

Hi Alexey,

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

Regards,

Gayathri

0

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

0
Mandy Krimmel
Mar 23, 2017 at 11:09 AM
1

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

Share
10 |10000 characters needed characters left characters exceeded
Mark Bernabe Mar 25, 2017 at 09:45 AM
1

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.

Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

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

0
Skip to Content