Consuming JSON based REST APIs in ABAP

Consuming JSON based REST APIs in ABAP

Understanding the Consuming JSON based REST APIs in ABAP. You realize that we have one of the biggest SAP Specialized Wire bunch with in excess of 5300 Experts and developing each spending day. Ask-Reply, Propose-Shield, Offer and Learn is our aphorism. Consistently various inquiries are posed, few get replied and many are left open for additional examination and investigation. Consistently we gain some new useful knowledge and we generally have something to contemplate over.

In one of these conversations, one part had a need to peruse information from Web. This article is a consequence of the example code scrap I wrote to help him. Consuming JSON based REST APIs in ABAP.

This is the determination screen of my test program to peruse information from a Website page.

I have attempted to utilize all the new grammar of ABAP and would like the freshers in new ABAP to investigate beneath bits.

1. String Concatenation Operation with COND #

lv_url = |http{ COND #( WHEN p_ssl EQ abap_true
THEN |s| ) }://gEtDaTaFrOmWeB.php{
COND #( WHEN p_id IS NOT INITIAL
THEN |?id={ p_id }| ) }|.

Check, link is finished utilizing pipe (|). Likewise actually look at the COND # which can be remembered for the string exacting tasks as well. At the point when P_SSL has information then ‘s’ is linked after ‘http’.

Likewise when P_ID is passed, the worth of P_ID which come from determination screen is linked toward the End.

Let’s check the Output.

1. When nothing is passed from the Selection Screen.

2. When the P_ID is passed and P_SSL is Selected.

‘s’ is appended after http to make it https. Also, ‘?id=1’ is appended to the end of the URL.

2. Inline Declaration in Class Method Calls

We really want to take note of that, Inline Announcement isn’t workable for Capability Module calls.

3. How to Convert JSON data to ABAP Internal Table?

/ui2/cl_json=>deserialize(
  EXPORTING
    json  = lv_data
  CHANGING
    data  = gt_customer ).

In the event that you don’t have the foggiest idea how the profound construction needs to seem to be, you can utilize the underneath code scrap to study the got information and set up your CHANGING design TYPE likewise.

TRY.
    DATA: lo_data TYPE REF TO data.
    DATA(lo_json) = NEW /ui2/cl_json( ).
    lo_json->deserialize_int(
      EXPORTING
        json = lv_data
      CHANGING
        data  = lo_data ).
  CATCH cx_sy_move_cast_error INTO DATA(lx_sy_move_cast_error).
ENDTRY.

When you have the lo_data, actually take a look at it in troubleshoot mode and afterward re-compose your code to pronounce the Sort you really want.

4. How to Convert ABAP Internal Table data to JSON?

DATA(lv_json_body) =  /ui2/cl_json=>serialize(
                        data        =  ls_abap_data
                        pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).

Despite the fact that serialize technique was not required in our example report to peruse Web Information, however since we were discussing JSON to ABAP Inner table, I figured it would be helpful assuming we likewise showed the opposite way around.

TYPES:
  BEGIN OF ts_deep_entity,
    mat_doc    TYPE matnr,
    doc_year   TYPE gjahr,
    pstng_date TYPE sy-datum,
    ernam      TYPE sy-uname,
  END OF ts_deep_entity.

TYPES:
  BEGIN OF ty_head,
    d TYPE ts_deep_entity,
    e TYPE ts_deep_entity,
  END OF ty_head.

*======================ABAP Data==================================
DATA(ls_abap_data) = VALUE ty_head( d = VALUE #( mat_doc = '4900000814'
                                                 doc_year   = '2019' 
                                                 pstng_date = '20190701' 
                                                 ernam     =  'kuldeep' )
                                    e = VALUE #( mat_doc = '4900000815' 
                                                 doc_year   = '2020' 
                                                 pstng_date = '20200701' 
                                                 ernam     =  'joshi' ) ).
*======================ABAP to JSON==================================
DATA(lv_json_body) = /ui2/cl_json=>serialize(
                       data        =  ls_abap_data
                       pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).

Assuming you look carefully, the JSON information has been organized in CamelCase.

Likewise, we can design the JSON yield in various ways:

  • Camel Case
  • Extended
  • Low Case
  • None
  • User
  • User Low Case

We will commit a different article on ABAP to JSON and JSON to ABAP design. Today, we needed to gain a straightforward method for perusing information from Web and show in SAP ABAP. With all the above information, we have composed the underneath program. Allow us to test it.

Selection Screen

Output 1

Input 2 and Output 2

Complete Working Code for Reference

REPORT zstkoes_get_data_from_web.

TYPES:
BEGIN OF gty_customer,
id TYPE string,
firstname TYPE string,
name TYPE string,
country TYPE string,
END OF gty_customer .
TYPES:
gtty_customer TYPE TABLE OF gty_customer.

CONSTANTS:
gc_proxy_host TYPE string VALUE 'PROXY_HOST',
gc_proxy_service TYPE string VALUE '8090'.

DATA:
gs_customer TYPE gty_customer,
gt_customer TYPE gtty_customer.

SELECTION-SCREEN:
BEGIN OF LINE,
COMMENT 1(15) c_id.
PARAMETERS:
p_id TYPE string VISIBLE LENGTH 2.
SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN: COMMENT /1(50) c_info,
SKIP,
BEGIN OF LINE.
PARAMETERS: p_ssl TYPE xfeld.
SELECTION-SCREEN: COMMENT 3(50) c_ssl,
END OF LINE.

SELECTION-SCREEN: BEGIN OF LINE.
PARAMETERS: p_proxy TYPE xfeld.
SELECTION-SCREEN: COMMENT 3(50) c_proxy,
END OF LINE.


*----------------------------------------------------------------------*
* INITIALIZATION.
*----------------------------------------------------------------------*
INITIALIZATION.
c_id = 'ID'.
c_info = 'Leave empty to get a list of customer.'.
c_ssl = 'Use SSL (HTTPS)'.
c_proxy = 'Use Proxy'.

*----------------------------------------------------------------------*
* START-OF-SELECTION.
*----------------------------------------------------------------------*
START-OF-SELECTION.

TRY.

DATA(lv_url) = |http{ COND #( WHEN p_ssl EQ abap_true
THEN |s| )
}://gEtDaTaFrOmWeB.php{
COND #( WHEN p_id IS NOT INITIAL
THEN |?id={ p_id }| ) }|.

cl_http_client=>create_by_url(
EXPORTING
url = lv_url
proxy_host = COND #( WHEN p_proxy EQ abap_true
THEN gc_proxy_host )
proxy_service = COND #( WHEN p_proxy EQ abap_true
THEN gc_proxy_service )
sap_username = sy-uname
IMPORTING
client = DATA(lo_client)
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4 ).
IF lo_client IS BOUND.
lo_client->send(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4 ).
IF sy-subrc NE 0.
lo_client->get_last_error(
IMPORTING
message = DATA(cv_error_msg) ).
MESSAGE cv_error_msg TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
lo_client->receive(
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4 ).
IF sy-subrc NE 0.
lo_client->get_last_error(
IMPORTING
message = cv_error_msg ).
MESSAGE cv_error_msg TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
lo_client->response->get_status(
IMPORTING
code = DATA(lv_status) ).
IF lv_status = 200.
DATA(lv_data) = lo_client->response->get_cdata( ).
IF lv_data IS INITIAL.
MESSAGE 'No Data found' TYPE 'S' DISPLAY LIKE 'E'.
LEAVE LIST-PROCESSING.
ENDIF.
IF p_id IS INITIAL.
" Copy JSON-Data into internal table
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_data
CHANGING
data = gt_customer ).
ELSE.
" Copy JSON-Data into internal table
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_data
CHANGING
data = gs_customer ).
APPEND gs_customer TO gt_customer.
ENDIF.
ENDIF.
ENDIF.
CATCH cx_root.
ENDTRY.

*----------------------------------------------------------------------*
* END-OF-SELECTION.
*----------------------------------------------------------------------*
END-OF-SELECTION.

* Output the data
TRY.
cl_salv_table=>factory(
IMPORTING
r_salv_table = DATA(gref_salv)
CHANGING
t_table = gt_customer ).

CAST cl_salv_column(
gref_salv->get_columns( )->get_column( 'ID' )
)->set_medium_text( 'ID' ).
CAST cl_salv_column(
gref_salv->get_columns( )->get_column( 'FIRSTNAME' )
)->set_medium_text( 'Firstname' ).
CAST cl_salv_column(
gref_salv->get_columns( )->get_column( 'NAME' )
)->set_medium_text( 'Lastname' ).
CAST cl_salv_column(
gref_salv->get_columns( )->get_column( 'COUNTRY' )
)->set_medium_text( 'Country' ).

gref_salv->display( ).
CATCH cx_salv_msg.
CATCH cx_salv_not_found.
ENDTRY.

 

YOU MAY BE INTERESTED IN

Understanding the Role of an SAP Application Support Specialist

Tips for Building Custom SAP Applications: A Comprehensive Guide

Cracking the Code: Your Earning Potential as a SAP ABAP Developer with 5 Years of Experience

X
WhatsApp WhatsApp us