Wednesday 29 January 2020

Working with JavaScript Object Notation (JSON) format in ABAP (Serialization – Deserialization)

This blog post will give an overview of working with JavaScript Object Notation (JSON). We will create JSON string output from the internal table and read JSON data as input in ABAP program. Nowadays JSON format preferred over XML format due to lightweight and simple syntax.

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.

SAP ABAP Tutorials and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam, SAP ABAP Prep

[{“carrid”:”AA”,”connid”:17,”fldate”:”2017‑12‑07″,”price”:422.94},{“carrid”:”AA”,”connid”:17,”fldate”:”2018‑01‑08″,”price”:422.94},{“carrid”:”AA”,”connid”:17,”fldate”:”2018‑02‑09″,”price”:422.94},{“carrid”:”AA”,”connid”:17,”fldate”:”2018‑03‑13″,”price”:422.94}]

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

SAP ABAP Tutorials and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam, SAP ABAP Prep

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.

No comments:

Post a Comment