Wednesday, December 18, 2013

External Service call using JSON

JSON (/ˈdʒeɪsɒn/ jah-soun, /ˈdʒeɪsən/ ja-son), or JavaScript Object Notation, is an open standard format that uses human-readable text to transmit data objects consisting of attribute–value pairs. It is used primarily to transmit data between a server and web application, as an alternative to XML.
Did you ever wonder how to call an external webservice using JavaScript Object Notation (JSON)? If you are on release 7.02 or 7.03/7.31 (Kernelpatch 116) JSON is supported natively in ABAP.

It takes just a few lines of code to transform a structure or table to JSON format, after that you probably only have to worry about some formatting and escaping of special characters.

Convert the data to JSON format

The first step is to gather all the data into an internal table or structure depending on the information you have to send to an external party. Since that shouldn’t be a problem I pretend that the data is already available in table lt_data_table (This is an internal table with a structure that you need).

Above you see the use of lr_writer as target for the call transformation, because the type of this writer is set to if_sxml=>co_xt_json the data will be transformed into JSON-XML format. Since the output of lr_writer is an xstring, let’s use ‘ECATT_CONV_XSTRING_TO_STRING’ to convert the xstring to a string. So by now the internal SAP table or structure is converted into a string containing the data in standard JSON format.

For this blog I use an internal table (with one line) with a structure containing the fields company_name, adres, postcode, woonplaats, land and email. This will be converted to a string containing:
{"TEXT":[{"COMPANY_NAME":"Acorel B.V.","ADRES":"Zwanebloemstraat 83","POSTCODE":"1087GD","WOONPLAATS":"Amsterdam","LAND":"Nederland",


Formatting the data

The second step is about the formatting of the eventual JSON string, besides thinking about the escape characters this also depends on the requirements of the external party that the data will be send to.

Because the names of the key (of the key-value pairs) are important in this case, I wrote a piece of logic to transform e.g. COMPANY_NAME to Company Name.

In the code above the variable lv_json_start will be filled with the order of the attributes. In this example the content of lv_json_start will be:

"Company Name":"0","Adres":"1 ","Postcode":"2","Woonplaats":"3","Land":"4","Email":"5"
And the content of lv_json is converted to:

{"TEXT":[{"Company Name":"Acorel B.V.","Adres":"Zwanebloemstraat 83","Postcode":"1087GD","Woonplaats":"Amsterdam","Land":"Nederland","Email"}]}
Now we’re almost there for the formatting part but because SAP proposes TEXT as the name for the array of values this field will be replaced with 'customers' in our example.

So now we have the variable lv_json in the format it has to be for the call to the external party.

{"attributeOrder":{"Company Name":"0","Adres":"1 ","Postcode":"2","Woonplaats":"3","Land":"4","Email":"5"},"customers":[{"Company Name":"Acorel B.V.","Adres":"Zwanebloemstraat 83","Postcode":"1087GD","Woonplaats":"Amsterdam","Land":"Nederland","Email"}]}

If you want to be sure whether your JSON string is correctly formatted, you can check this on several online JSON validator sites such as

Another advantage of these online validators is that you’re quickly able to check if the special characters are correctly escaped. For more information about escaping special characters have a look at

Send the data

The final step is to send the data.
In the code above the variable lv_result will contain information about the result of the service call.

No comments:

Post a Comment