Skip to Content
avatar image
-2
Former Member

JSON String to XML in ABAP

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

Add comment
10|10000 characters needed characters exceeded

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

  • 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!

  • Get RSS Feed

3 Answers

  • Best Answer
    Mar 06, 2017 at 04:18 PM

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

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    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>

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 06, 2017 at 11:58 AM

    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 ).
    
    Add comment
    10|10000 characters needed characters exceeded