Skip to Content

How to convert JSON response from REST API to ABAP Structures?

Dec 12, 2017 at 09:52 PM


avatar image
Former Member

Hi Folks,

I am calling a REST API from ABAP code. I am able to get the HTTP response. But the response from REST API is coming in JSON format. Is there a way to convert the JSON response which has nested deep structures to ABAP Internal table?

I need to convert it to ABAP table to loop across the information before processing it.



10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Ankit Maskara
Dec 13, 2017 at 05:52 AM

Check here

10 |10000 characters needed characters left characters exceeded
avatar image
Former Member Dec 12, 2017 at 10:12 PM

Hi Narayan,

SAP has an API class CL_FDT_JSON which allows to convert the JSON response to ABAP structure. I noticed this class is better than /UI2/CL_JSON. The CL_FDT_JSON converts the deep table structure without field names and just values as well to ABAP structure. Had to use CL_REST_HTTP_CLIENT to obtain the string information from REST API.

Note: The ABAP SE11 structure should exactly match the JSON response received from the REST API. Only then the conversion works.

I have attached a sample code below which was used by me to achieve the JSON to ABAP conversion.

* Declarations
    DATA lo_client         TYPE REF TO if_http_client.
    DATA lo_conv           TYPE REF TO cl_abap_conv_in_ce.
    DATA lo_rest_client    TYPE REF TO cl_rest_http_client.
    DATA lo_response       TYPE REF TO if_rest_entity.

    DATA lv_result_url     TYPE string.
    DATA lv_url            TYPE string.
    DATA lv_json           TYPE string.
    DATA lv_response       TYPE string.
    DATA lv_httpcode       TYPE i.
    DATA lv_reason         TYPE string.
    DATA lv_message        TYPE string.
    DATA lv_index          TYPE i.
    DATA lt_data_json      TYPE zxyz_geocode_response. "Exact structure of JSON response created in SE11
    DATA response          TYPE string.

    CALL METHOD cl_http_client=>create_by_destination
        destination              = 'ZXYZ_GEOCODE'
        client                   = lo_client
        argument_not_found       = 1
        destination_not_found    = 2
        destination_no_authority = 3
        plugin_not_active        = 4
        internal_error           = 5
        OTHERS                   = 6.
    IF sy-subrc <> 0.


    IF lo_client IS BOUND.
      lo_client->request->set_method( if_http_request=>co_request_method_get ).

* Construct the URL
        CONCATENATE '?housenumber=' im_address-house_num1
                    '&street='      im_address-street
                    '&city='        im_address-city1
                    '&postalcode='  im_address-post_code1
                    '&state='       im_address-region
                    '&country='     im_address-country
                    INTO lv_result_url.

* Set URL to request
        cl_http_utility=>set_request_uri( request = lo_client->request uri = lv_result_url ).

* Send the HTTP request
      CALL METHOD lo_client->send
          http_communication_failure = 1
          http_invalid_state         = 2
          http_processing_failed     = 3
          http_invalid_timeout       = 4
          OTHERS                     = 5.
      IF sy-subrc <> 0.


* HTTP call receive
      CALL METHOD lo_client->receive
          http_communication_failure = 1
          http_invalid_state         = 2
          http_processing_failed     = 3
          OTHERS                     = 4.
      IF sy-subrc <> 0.


*   get status of the response
      CALL METHOD lo_client->response->get_status
          code   = lv_httpcode
          reason = lv_reason.

* Instantiate REST client
      CREATE OBJECT lo_rest_client
          io_http_client = lo_client.

* Get Response data
      lo_response = lo_rest_client->if_rest_client~get_response_entity( ).

* Get string data
      response = lo_response->get_string_data( ).

* Convert string to Abap structures (better than /UI2/CL_JSON, converts even tables without field names and just values)
      CALL METHOD cl_fdt_json=>json_to_data
          iv_json = response
          ca_data = lt_data_json.

      CALL METHOD lo_client->close
          http_invalid_state = 1
          OTHERS             = 2.
      IF sy-subrc <> 0.



Joseph M

10 |10000 characters needed characters left characters exceeded