How to Prepare JSON in ABAP & Send Response to Cloud?

4
4501
json response in abap on cloud

In this article we will learn how to prepare JSON in ABAP using utility classes and we will send our JSON response to UI with the help of API destinations. We will use SM59 destination for the setting up of connection with the API.

How we are going to proceed?– Suppose we have database table with particular records, we will prepare JSON format using the table records by storing it in internal table. Now we will send the JSON response to cloud, where the data will be mapped to UI.UI will use this JSON to bind the field to the frontend which will be usable for development of the application. Now lets start!

Step1: Lets prepare a table with proper fields as that of UI in the table. To create the table T-code is SE11. Let us suppose that we have created following table “ZCUSTOMER_DATA” with the key field as CustomerID, Name, Age as follows:

Suppose it has following data:

Step 2: Now lets create a report to prepare JSON of this table using T-code SE38.

These lines of code will select data from database table ZCUSTOMER_DATA into the internal table lt_customerData. Here we will be using utility class /ui2/cl/json=>serialize for preparing of JSON from internal table. JSON will look like as follows:

Next step is to configure SM59 destination.

Step 3: Lets configure SM59 destination with host URL, client id and password. Suppose that we have created SM59 destination as ‘ZSEND’. To Create SM59 destination, click on create as follows:

Enter the destination name and choose the connection type ,here we will use G type connection, because here we are going to connect to external SAP server. Click ok.

Enter the description , host, port and path prefix to cloud API as shown below. All these information you will get from CPI(cloud team).

You can also configure the password by clicking on “Logon & Security” tab as follows. You need to select basic authentication for providing user or password and then save the connection. You can also test the connection by clicking on “Test Connection” Tab at top-left corner. If connection is successful you get 200 in response which is ‘OK’.

Step 4: Lets continue to code to send the to UI in Cloud. The code will look like as :

  1. Data Declarations:

2. Create URL using SM59 destination as follows:

3. Set the method as POST, Set the header fields and set the data using JSON.

Data will be replicated to cloud assuming CPI is integrated as a middle ware.

*---------------------------------------------------------
* Author : Ranbir Singh
* Companay : TAC GBS (SAPYard)
* Description : Send JSON input to the Cloud from SAP ABAP
*---------------------------------------------------------
REPORT zsend_to_cloud.

DATA: lo_http_client   TYPE REF TO if_http_client,
      gv_json          TYPE string,
      lv_port          TYPE   rfcdisplay-rfcsysid,
      lv_server        TYPE rfcdisplay-rfchost,
      lv_httpcode      TYPE i,
      lv_response      TYPE string,
      lv_reason        TYPE string.

CONSTANTS: lc_content    TYPE string VALUE 'Content-Type',
           lc_contentval TYPE string VALUE 'application/json'.


SELECT * FROM  zcustomer_data INTO TABLE @DATA(lt_customerData).
DATA(lv_json) = /ui2/cl_json=>serialize( lt_customerData ).

cl_demo_output=>display_json( lv_json ).

CALL METHOD cl_http_client=>create_by_destination
  EXPORTING
    destination              = 'ZSEND'
  IMPORTING
    client                   = lo_http_client
  EXCEPTIONS
    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.
* Implement suitable error handling here
ENDIF.

  lo_http_client->refresh_request( ).

  lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_0 ).

  CALL METHOD lo_http_client->request->set_method
    EXPORTING
      method = lo_http_client->request->co_request_method_post.

* set header fields
  CALL METHOD lo_http_client->request->set_header_field
    EXPORTING
      name  = lc_content
      value = lc_contentval.



* set the json data to cloud
  CALL METHOD lo_http_client->request->set_cdata
    EXPORTING
      data = lv_json.
  
*  send the data
  lo_http_client->send(
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2 ).

* receive the response to verify data is sent successfully or not
  lo_http_client->receive(
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3 ).


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

*if 200 then success
  WRITE: lv_httpcode.

This was a short and crisp post to to make you familiar with the flow and programming part to send the JSON data to Cloud.

This is my first article on SAPYard. Please do share your feedback.

Comments Please

Please follow our LinkedIn Page,LinkedIn Group, Facebook Page, Facebook Group, Twitter , Instagramand Telegram SAP Technical Group.

Do not forget to SUBSCRIBE to our YouTube Channel for Free Courses and Unconventional Interesting Videos.

Also consider joining SAPYard’s Telegram Channel for SAP Blogs and Tutorials.

Save our number +1-251-727-9273 and send us a Whatsapp message ‘LEARN’ to be part of our Learning Community.

Some other Tutorials on JSON

Step by Step Tutorials on Web Services in SAP

4 COMMENTS

  1. I have a suggestion as below : then you dont have to type send and receive. User the cl rest http client

    ” Create http client
    cl_http_client=>create_by_url( EXPORTING url = {}
    IMPORTING client = DATA(lo_http_client) ).
    ” Get rest client
    DATA(lo_rest_client) = NEW cl_rest_http_client( lo_http_client ).

    ” Create API request
    DATA(lo_request) = lo_rest_client->if_rest_client~create_request_entity( ).
    lo_request->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_json ).
    CONCATENATE LINES OF lt_files INTO DATA(lv_error_files) SEPARATED BY cl_abap_char_utilities=>cr_lf.
    lo_request->set_string_data( |\{‘text’ : ‘List of files in error folder \n{ lv_error_files }’\}| ).

    ” Call to API
    lo_rest_client->if_rest_resource~post( lo_request ).

LEAVE A REPLY

Please enter your comment!
Please enter your name here