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: 

How do I convert a JSON response to XML in ABAP

former_member183993
Participant
0 Kudos

hi experts,

I am implementing a web service consumer where I am getting a response in JSON.  I need to make it readable in SAP so I figured I could find some way to convert the JSON response to XML and then map it to an ABAP structure.  I have found tons of posts on this but none of them seem clear to be the outcome that I wish to achieve.  I am actually shocked there is no standard class to do this, although I have read from others to call transformation id passing in the json and get back xml but perhaps our kernel is not the most up to date.

Anyhow, I would certainly appreciate any help.

Thanks,

Chris

1 ACCEPTED SOLUTION

former_member192050
Participant
0 Kudos

Hi Chris,

in that URL, type $format=xml it will convert that to xml file

Regards

Sathish

11 REPLIES 11

custodio_deoliveira
Active Contributor
0 Kudos

Hi,

You can convert the Json directly to ABAP internal table, no need to the XML step.

Regards,

Custodio

former_member192050
Participant
0 Kudos

Hi Chris,

in that URL, type $format=xml it will convert that to xml file

Regards

Sathish

0 Kudos

hi Sathish,

I tried that but it doesnt work, says resource is not available.

thx

Chris

0 Kudos

Of course it doesn't work, unless you have an OData service...

Have you read the links I sent you? Converting from JSON to ABAP internal table is as simple as it can be. Again: no stupid conversion to XML step is needed. Very simple example:

REPORT zjson.

TYPES:

   BEGIN OF ty_sales_order,

     so_number TYPE vbeln_va,

     sales_org TYPE vkorg,

     order_date TYPE datum,

     END OF ty_sales_order.

DATA sales_order TYPE ty_sales_order.

DATA json TYPE string VALUE '{"SALES_ORDER": {"SO_NUMBER": "2000000010","SALES_ORG": "SO01","ORDER_DATE":"2014-10-31"}}'.

CALL TRANSFORMATION id SOURCE XML json RESULT sales_order = sales_order .

***************************

Same example as an internal table:

DATA sales_orders TYPE TABLE OF ty_sales_order.

DATA json TYPE string VALUE '{"SALES_ORDERS": [{"SO_NUMBER": "2000000010","SALES_ORG": "SO01","ORDER_DATE": "2014-10-31"},{"SO_NUMBER": "2000000020","SALES_ORG": "SO02","ORDER_DATE": "2014-11-05"}]}'.

CALL TRANSFORMATION id SOURCE XML json RESULT sales_orders = sales_orders .

Regards,

Custodio

0 Kudos

Custodio,

Thanks!  your explanation helped me out greatly.  The JSON I receive does not have all of the components that yours had so I added one for the transformation to work, also had to translate to upper in order to work.

Again, many thanks!

Chris

0 Kudos

Thanks Custodio. It helped me too.

0 Kudos

Hello Custodio,

Thanks for your answer, it looks very simple using transformation. but when I try the json with lowercase tag, it sames not working... for example:

DATA lv_json TYPE string.

DATA lv_token TYPE string.

lv_json = '{"access_token":"token"}'.

CALL TRANSFORMATION id SOURCE XML lv_json RESULT access_token = lv_token.


In my case, the json's tag in the lowercase letter. When I run this piece of code, I got nothing in lv_token.

Do you know how to handle this case?

Thank you.


Warm regards,

Rong

0 Kudos

Hi Rong,

I think you need to transform the names of JSON objects to uppercase letters first like it is shown in demo report 'demo_json_names_to_upper'. You can use the predefined transformation 'demo_json_xml_to_upper' (or also ' wdr_json_xml_to_upper ') to update your JSON string.

Maybe like this:

DATA(lo_writer) = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION wdr_json_xml_to_upper SOURCE XML lv_json
                                                                           RESULT XML lo_writer.
DATA(lv_json_upper) = lo_writer->get_output( ).
"JSON to ABAP
CALL TRANSFORMATION id SOURCE XML lv_json_upper
                                           RESULT access_token = lv_token.


Regards,

Bernd

0 Kudos

Hi Bernd,

Thanks for your answer. Can I use demo_json_xml_to_upper or wdr_json_xml_to_upper  in the project  and delivery to customer? Is these two tansformation a standard function?

Thank you,

Warm regards,

Rong

0 Kudos

as told by Bernd, you must do:

DATA lv_json TYPE string.
DATA lv_token TYPE string.
lv_json = '{"access_token":"token"}'.
CALL TRANSFORMATION id SOURCE XML lv_json RESULT access_token = lv_token.

You should also have created a new post to ask your question (with simply a reference to this thread if needed), otherwise people seeking for answers to the same question as yours won't see immediately the answer because you can't mark it if you post in another thread

Cheers

0 Kudos

Hi Rong,

I prefer WDR_JSON_XML_TO_UPPER to the Demo version although it belongs to the WebDynpro package.

Regards,

Bernd