Skip to Content
-1

JSON to ABAP

Feb 13 at 11:26 PM

165

avatar image
Former Member

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

10 |10000 characters needed characters left characters exceeded

Hint 1: share a sample of the JSON response.

1
0
Former Member

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.

0
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
avatar image
Former Member Feb 16 at 04:17 AM
0

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.

Share
10 |10000 characters needed characters left characters exceeded