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: 

JSON to ABAP

shaila_kasha
Contributor

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

1 ACCEPTED SOLUTION

shaila_kasha
Contributor
0 Kudos

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.

4 REPLIES 4

matt
Active Contributor

Hint 1: share a sample of the JSON response.

horst_keller
Product and Topic Expert
Product and Topic Expert

shaila_kasha
Contributor
0 Kudos

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.

shaila_kasha
Contributor
0 Kudos

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.