Now a days most of the API’s are in the form of Rest based services and the data that is retrieved from these services are in the form of JSON format. This tutorial will make you understand how to parse a JSON format data
The System has to be configured so that it can communicate with Rest based services. You can check whether the system is configured for consuming Rest based Services by going to transaction code STRUST. You should be able to see all the below mentioned configurations. If not, contact your BASIS for setting up the system in such a way.
If the API you are going to consume has some Certificates. Then import them as well into the SSL.
Create an RFC destination if needed and test it, whether it is pinging to the API that you have mentioned
lo_client type ref to if_http_client,
lv_result_url type string,
lv_bin type xstring,
lo_conv type ref to cl_abap_conv_in_ce,
lv_response type string.
if lv_matkt is not initial.
host = ‘<api name>’
service = ‘443’
scheme = 2
client = lo_client ).
lo_client->request->set_method( if_http_request=>co_request_method_get ).
‘<param 1>=<value 1>’
‘<param 2>=<value 2>’
lv_result_url respecting blanks.
cl_http_utility=>set_request_uri( request = lo_client->request uri = lv_result_url ).
lv_bin = lo_client->response->get_data( ).
lo_conv = cl_abap_conv_in_ce=>create( input = lv_bin ).
lo_conv->read( importing data = lv_response ).
data: lv_matchcount_positive type zsample,
lv_matchcount_negative type i,
lv_startpos type i,
lv_endpos type i.
find ‘”seriesName“:”PositiveSentiment“,”set”:[‘ in lv_response match offset lv_startpos.
find ‘]’ in section offset lv_startpos of lv_response match offset lv_endpos .
lv_startpos = lv_startpos + 40.
lv_endpos = lv_endpos – lv_startpos.
wa_netbase–category = ‘Positive Sentiments’.
wa_netbase–value = lv_response+lv_startpos(lv_endpos).
append wa_netbase to lt_output.
The above mentioned code mentions of how to call a API and then parse the data to get the desired output. First, you have to understand the output that come form the API. The output will have a certain format like, <parameter name>:<value>. The whole structure might differ based on the additional parameters you pass. So based on that you have to do string operations and functions to get the desired output. In my case the JSON format data has the parameter name as “positivesentiment’ and the values as ‘100’. So I used to search for the key word and then find the value that is present after the “:”.