Skip to Content
-2

JSON String to XML in ABAP

Mar 06, 2017 at 11:35 AM

139

avatar image

Hello All, I have a JSON string, It is a complex JSON with nodes inside nodes. I want to create an XML from this JSON Object. And use this XML for further processing.

I used Transformation method

writer = cl_sxml_string_writer=>create( ).

CALL TRANSFORMATION id SOURCE text = json_string RESULT XML writer.

xml = cl_abap_codepage=>convert_from( writer->get_output( ) ).

but it's not working. could you please help me

my JSON String is like that

"root": { "QueryCC_WOSPECResponse": {

"ACTLIST": {

"ACT": {

"element": [

{

"Attributes": {

"ACTINT": {

"content": "0.0"

},

"ACTHRS": {

"content": "0.0"

},

"ACTIDID": {

"content": "12345",

"resourceid": "true"

}

},

"RelatedMbos": {

"ACTSPEC": {

"element": [

{

"Attributes": {

"ATTRID": {

"content": "CMINBGYJ"

},

"WORKORDERSPECID": {

"content": "12345",

"resourceid": "true"

}

}

},

{

"Attributes": {

"ALNVALUE": {

"content": "..."

},

"WORKORDERSPECID": {

"content": "1234",

"resourceid": "true"

}

}

},

"rsCount": "1",

"rsStart": "0"

}

}

}

Thank you

10 |10000 characters needed characters left characters exceeded

In addition to the answer given by Horst, you need to ensure that the json is having valid format. You can try pasting the json into jsonlint.com to validate it and fix the errors.

1

It seems you have a tiny problem but we don't have all information.

So, please provide a small fully-valid program so that we can help you quickly!

0
* Please Login or Register to Answer, Follow or Comment.

3 Answers

Best Answer
Horst Keller
Mar 06, 2017 at 04:18 PM
0

"I tried above Solution but it displaying XML Structure"

But that was exactly your demand:

"I want to create an XML from this JSON Object. And use this XML for further processing."

The XML structure resulting from transformation ID is JSON-XML, SAP's XML representation of JSON. What else did you expect? Now you can go on and write a transformation or a parser that transforms that result to the format you need.

Share
10 |10000 characters needed characters left characters exceeded
Anu P Mar 06, 2017 at 03:56 PM
-2

Hi Horst,

Thanks for your response

I tried above Solution but it displaying XML Structure

I need to convert that XML for further processing, could you please help me how to get this is XML string.

<object>

<object name=”is_model“>

<str name=”t”>DD</str>

<str name=”i”>Value fo DD</num>

</object>

.

.

.

.

</object>

Share
10 |10000 characters needed characters left characters exceeded
Horst Keller
Mar 06, 2017 at 11:58 AM
0

You have to write

CALL TRANSFORMATION id SOURCE XML json_string 
                       RESULT XML writer.

Working example:

DATA(json_string) = cl_abap_codepage=>convert_to(
 `{` &&
 ` "order":"4711",` &&
 ` "head":{` &&
 `  "status":"confirmed",` &&
 `  "date":"07-19-2012"` &&
 ` },` &&
 ` "body":{` &&
 `  "item":{` &&
 `   "units":"2", "price":"17.00", "Part No.":"0110"` &&
 `  },` &&
 `  "item":{` &&
 `   "units":"1", "price":"10.50", "Part No.":"1609"` &&
 `  },` &&
 `  "item":{` &&
 `   "units":"5", "price":"12.30", "Part No.":"1710"` &&
 `  }` &&
 ` }` &&
 `}` ).
CALL TRANSFORMATION id SOURCE XML json_string
                       RESULT XML DATA(xml).
cl_demo_output=>display_xml( xml ).
Share
10 |10000 characters needed characters left characters exceeded