Introduction
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
data:
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.
cl_http_client=>create(
exporting
host = ‘<api name>'
service = '443'
scheme = 2
importing
client = lo_client ).
lo_client->request->set_method( if_http_request=>co_request_method_get ).
concatenate
'https://<api name>.com/?'
'<param 1>=<value 1>'
'<param 2>=<value 2>’
into
lv_result_url respecting blanks.
cl_http_utility=>set_request_uri( request = lo_client->request uri = lv_result_url ).
lo_client->send( ).
lo_client->receive( ).
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 ).
lo_client->close( ).
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.
clear: lv_response,
lv_startpos,
lv_endpos,
wa_netbase.
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 “:”.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
2 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 | |
1 |