ABAP Format to json

Converting ABAP Internal Table to JSON & JSON to ABAP


The class /UI2/CL_JSON provides methods such as Serialize & Deserialize to convert ABAP to JSON and vice-versa.

Below code converts ABAP internal table to JSON String.

Output- JSON String

In above output the field names are in capital. We can use preety mode to make it different.

Pass – preety name and check the output.

The field names are now- lower case.

We can use Deserialize method to convert a JSON string to ABAP Internal table.

Output-


TYPES BEGIN OF ty_scarr,
carrid TYPE scarr-carrid,
carrname TYPE scarr-carrname,
END OF ty_scarr.

DATA lt_scarr1 TYPE TABLE OF ty_scarr,
lt_scarr2 TYPE TABLE OF ty_scarr,
lv_json TYPE string.

SELECT carrid, carrname FROM scarr INTO TABLE @lt_scarr1 UP TO 3 ROWS.

ui2cl_json=serialize(
EXPORTING
data = lt_scarr1 Data to serialize
pretty_name = ui2cl_json=pretty_mode-low_case Pretty Print property names
RECEIVING
r_json = lv_json ). JSON string

WRITE lv_json.

ui2cl_json=deserialize(
EXPORTING
json = lv_json JSON string
CHANGING
data = lt_scarr2 ). Data to serialize

cl_demo_output=display_data( lt_scarr2 ).


One comment

  1. Hi,
    I need a help,My request is
    Am feteching data from different tables and then appending into an internal table.
    Then from that internal table have to convert data to JSON format and have to sent the whole data from internal Table to a file(i.e AL11).But am getting only certain string into the file,not the whole data from the internal table.
    could you please help me out.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s