02-13-2018 11:26 PM
Hi experts,
I am trying to consume a rest API in abap. i get the response in json format (string).
I want to convert this output to internal structure. I tried all possible sap methods and scn links, but no luck.
In all trials 'response' if my json output and lt_data is the exact structure of the output from se11.
Try 1
CREATE OBJECT lr_json_deserializer.
lr_json_deserializer->deserialize( EXPORTING json = response IMPORTING abap = lt_data ).
reason: sap interprets 'city' attribute as , city = " dublin ", but json reads as "city" : "dublin"
Try 2
CALL TRANSFORMATION id SOURCE XML response
RESULT text = lt_data.
reason: no matter what no response
Try 3
CALL METHOD cl_fdt_json=>json_to_dataEXPORTING
iv_json = responseCHANGING
ca_data = lt_data.
reason: take only 1 attribute and if it is more than one treats as only 1 string.
Anyone with hints is highly appreciated.
Thank you very much. Shaila
02-16-2018 4:17 AM
Hi all,
I was able to solve this by using same class below. the only difference is that we need to append/concatenate the service request details in the receiving string value..
CALL METHOD cl_fdt_json=>data_to_json
EXPORTING
ia_data = ls_request
RECEIVING
rv_json = lv_body.
Thank you all.
02-14-2018 8:18 AM
02-14-2018 8:32 AM
Hint 2: Learn the basics of ABAP and JSON
https://help.sap.com/http.svc/rc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abenabap_json.htm
02-14-2018 10:28 PM
Thank you very much for the responses.
Below is sample response string:
data: lv_value type string value ' { "Field 1" : "value1 ", "field2" : "value2 "} '
I am able to convert json to abap using below class.
REPLACE ALL OCCURRENCES OF REGEX cl_abap_char_utilities=>cr_lf
IN lv_value WITH ''.
DATA: lt_data TYPE yresponse_strut. "this is the exact same structure as output defined in SE11.
CALL METHOD cl_fdt_json=>json_to_data
EXPORTING
iv_json = lv_value
CHANGING
ca_data = lt_data.
however now i have another problem. I need to send the request in JSON format also. however i have abap structure format. so i tried to use the below method. Although i get the format like string format into lv_body, same as lv_value, and when i pass the same response to REST API, i am not getting the response. Error says invalid json format. For some reasons below method is not giving te response with cl_abap_char_utilities=>cr_lf charaters and Json wants in this format.
CALL METHOD cl_fdt_json=>data_to_json
EXPORTING
ia_data = ls_request
RECEIVING
rv_json = lv_body.
any hints will be highly appreciated.
Thank you, shaila.
02-16-2018 4:17 AM
Hi all,
I was able to solve this by using same class below. the only difference is that we need to append/concatenate the service request details in the receiving string value..
CALL METHOD cl_fdt_json=>data_to_json
EXPORTING
ia_data = ls_request
RECEIVING
rv_json = lv_body.
Thank you all.