Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

JSON String to XML in ABAP

0 Kudos

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

1 ACCEPTED SOLUTION

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

"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.

5 REPLIES 5

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

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 ).

Former Member

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.

0 Kudos

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>

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

"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.

Sandra_Rossi
Active Contributor
0 Kudos

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!