03-06-2017 11:35 AM
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
03-06-2017 4: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.
03-06-2017 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 ).
03-06-2017 12:20 PM
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.
03-06-2017 3:56 PM
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>
03-06-2017 4: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.
03-06-2017 8:09 PM
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!