JSON consists of two-part information data first part as key fields (name attribute) and the second part as field value which separated by a colon “:”. This JSON output can be easily readable via any text edition or JSON viewer.
JSON format supported by many government organizations as a valid data format for upload e.g. EU VAT, GST portal.
In this blog post, I will create JSON output from the ABAP program (Part 1) and Convert JSON string input data to ABAP internal table (Part 2). When we work with JSON we came across two words as “serialization / Deserialization”. This serialization means converting input data structure(table) into JSON string format and Deserialization means to convert JSON string data into given structure format.
Now we explore below in more details with ABAP code.
JSON output from the ABAP program (Part 1 code): I have used “SFLIGHT” demo table for demonstration. I will be select 4 fields CARRID, CONNID, FLDATE and PRICE from “SFLIGHT” table.
*& Author: Shravankumar Tiwari * **This part 1- convert ABAP internal table data to JSON format DATA gv_json_output TYPE string. SELECT carrid, connid, fldate, price FROM sflight INTO TABLE @DATA(it_sflight) UP TO 4 ROWS. **JSON converter class - Method -> Serialize method to convert data in JSON gv_json_output = /ui2/cl_json=>serialize( data = it_sflight compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ). **Now check JSON output converted format cl_demo_output=>display( gv_json_output ).
We get JSON output for these 4 records as shown below.
Convert JSON string input data to ABAP internal table (Part 2): First, we need to create a structure to map JSON string data. Deserialization we need to convert string data to given internal table or structure if a single record needs to decode from JSON string.
Now we call “DESERIALIZE” method with all required input parameters exporting and changing data internal table. We fill JSON string name in exporting and convert data to internal table “ITB_SFLIGHT_INPUT” format.
*& Author: Shravankumar Tiwari * **This part 2 - convert JSON data to ABAP internal table TYPES: BEGIN OF str_sflight_input, carrid TYPE s_carr_id, connid TYPE s_conn_id, fldate TYPE s_date, price TYPE s_price, END OF str_sflight_input. DATA itb_sflight_input TYPE STANDARD TABLE OF str_sflight_input. CLEAR itb_sflight_input. **JSON converter class - Method -> Deserialize convert JSON string data into internal table /ui2/cl_json=>deserialize( EXPORTING json = gv_json_output pretty_name = /ui2/cl_json=>pretty_mode-camel_case CHANGING data = itb_sflight_input ). **Internal table filled from JSON input data cl_demo_output=>display( itb_sflight_input ).
We get the output as shown below
Now we can conclude by using standard ABAP class “/UI2/CL_JSON” we can create JSON string output from the internal table using the method “serialize” and convert JSON string data to ABAP internal table using the method “deserialize”. You can also use deep structure internal table this JSON conversion works both ways. Now you can refer sample code when working with a JSON object.
You can also refer JSON syntax on https://www.json.org/
Nice easy to follow examples!
/UI2/CL_JSON -> https://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer
For information, there is an option to colorize your code, to make your blog post more attractive.
usefully example. thanks.
Thank you very much for your sample!