In this tutorial the bitcoin exhange rates from http://blockchain.info will be read by a small SAP program and exported as SAP standard list output. To call the external JSON REST service the json4sapnw project was used. The target group for this blog are ABAP developers. Newbies are welcome.
The scenario
Calling the URL https://blockchain.info/de/ticker a json output will be generated including the exhange rates for bitcoins for different currencies. The api is described here.
The output
After reading the current rates from the blockchain site a typical SAP list output is generated.
The ABAP code
The example program is a standard report created by transaction SE38.
REPORT ZJSON_EXAMPLE_BITCOIN_RATES NO STANDARD PAGE HEADING. * ---------- local data DATA: lv_url TYPE string VALUE 'http://blockchain.info/de/ticker'. DATA: lr_json TYPE REF TO /cex/if_jmp_json. DATA: lr_request TYPE REF TO /cex/cl_jmp_json_request. DATA: lr_answer TYPE REF TO /cex/cl_jmp_json_object. DATA: lr_object TYPE REF TO /cex/cl_jmp_json_object. DATA: lr_array TYPE REF TO /cex/cl_jmp_json_array. DATA: lr_data TYPE REF TO /cex/cl_jmp_json_data. DATA: lv_sell TYPE /cex/jmp_json_double. DATA: lv_buy TYPE /cex/jmp_json_double. DATA: lv_string TYPE string. DATA: lv_symbol TYPE string. START-OF-SELECTION. * ---------- create a new client lr_request ?= /cex/cl_jmp_json_request=>create_with_url( lv_url ). IF lr_request IS NOT INITIAL. IF lr_request->execute( ) EQ abap_true AND lr_request->is_response( ) EQ abap_true AND lr_request->is_response_json( ) EQ abap_true. * get the output as json object lr_json ?= lr_request->get_response_json( ). * get the json as "object" lr_answer ?= lr_json. * get all keys as array object lr_array ?= lr_answer->names( ). * output header line WRITE: / 'symbol', AT 38 'sell', AT 64 'buy'. * loop all keys WHILE lr_array->has_next( ) EQ abap_true. * get the key and get it as a string lr_data ?= lr_array->next( ). lv_string = lr_data->get_string( ). * get the object behind the key lr_object = lr_answer->get_object( lv_string ). * get the rate info lv_sell = lr_object->get_double( 'sell' ). lv_buy = lr_object->get_double( 'buy' ). lv_symbol = lr_object->get_string( 'symbol' ). * output rate info WRITE: / lv_string, lv_symbol, AT 20 lv_sell DECIMALS 3 EXPONENT 0, AT 45 lv_buy DECIMALS 3 EXPONENT 0. ENDWHILE. ENDIF. * destroy the client lr_request->destroy( ). ENDIF.
The code does not contain any error handling like catching exceptions or process possible errors like unavailable website. See the demo for getting current weather conditions from openweather.org for a more complex implementation.
The project behind: json4sapnw
This main idea behind the project json4sapnw is to simplify the JSON handling like known from other programming languages. In the newer version released in 2017 a small JSON REST client implementation is included too.
The project can be used for free and it is implemented in the customer exchange namespace /CEX/. Some other demos can be found at the project website.