11-05-2014 2:44 AM
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
11-05-2014 4:43 AM
Hi Chris,
in that URL, type $format=xml it will convert that to xml file
Regards
Sathish
11-05-2014 4:17 AM
11-05-2014 4:43 AM
Hi Chris,
in that URL, type $format=xml it will convert that to xml file
Regards
Sathish
11-06-2014 1:30 PM
hi Sathish,
I tried that but it doesnt work, says resource is not available.
thx
Chris
11-06-2014 10:54 PM
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
11-07-2014 2:44 AM
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
06-01-2015 6:42 AM
03-15-2016 7:43 AM
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
03-27-2016 10:25 PM
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
03-28-2016 2:44 AM
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
03-28-2016 8:52 AM
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
03-28-2016 4:11 PM
Hi Rong,
I prefer WDR_JSON_XML_TO_UPPER to the Demo version although it belongs to the WebDynpro package.
Regards,
Bernd