Deserialize and Serialize JSON

I will explain how to you can work witj Javascript Notation or JSON objects using String variable, workarea and inertal table.

Please try to test this simple code and debug to show you how deserialize JSON objects from REST API SERVICE.

IMPORTANT:

Maybe you will need to implement follow SAP NOTES to use /ui2/cl_json=>serialize if you get any error like this:

Deserialize unknown JSON structure with /UI2/CL_JSON
Solution:

SAP NOTES:

0002292558
0002300508
0002330592
0002368774
0002382783
0002429758

Internal table to JSON

First to all we  will select information and convert this information on Json to after, convert this json in an internal table again,

SELECT carrid, connid, fldate, price
  FROM  sflight
  INTO TABLE @DATA(it_sflight)
  UP TO 1 ROWS.
**Now we convert data geted to a json
gv_json_output = /ui2/cl_json=>serialize( data = it_sflight compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).
*We print this information:
cl_demo_output=>display( gv_json_output ).

As you can see we now have a JSON like this:
abap internal table to JSON

Convert JSON to internal table

Its so easy to, just need to use other unction, we are goint to show yo.

Based on same example we will use our variable that contains our json:

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 itab TYPE STANDARD TABLE OF str_sflight_input.
CLEAR itab[].

"Convert JSON to Internal table
/ui2/cl_json=>deserialize( EXPORTING json = gv_json_output  pretty_name = /ui2/cl_json=>pretty_mode-camel_case  CHANGING data = itab ).
BREAK-POINT.

When you execute this program you will see on debug itab, this table contains data from json:

internal table json ABAP

Full code:

DATA gv_json_output TYPE string.

SELECT carrid, connid, fldate, price
  FROM  sflight
  INTO TABLE @DATA(it_sflight)
  UP TO 1 ROWS.
**Now we convert data geted to a json
gv_json_output = /ui2/cl_json=>serialize( data = it_sflight compress = abap_true pretty_name = /ui2/cl_json=>pretty_mode-camel_case ).
*We print this information:
cl_demo_output=>display( gv_json_output ).

BREAK-POINT.


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 itab TYPE STANDARD TABLE OF str_sflight_input.
CLEAR itab[].

"Convert JSON to Internal table
/ui2/cl_json=>deserialize( EXPORTING json = gv_json_output  pretty_name = /ui2/cl_json=>pretty_mode-camel_case  CHANGING data = itab ).
BREAK-POINT.

Its so easy right? if you have a JSON with children you need to define estructure like your json structure.

I hope this help you, if not, please let me know your question on the comments and I will rpley your doubs.

Regards and see you on  next.

Leave a Reply

Your email address will not be published. Required fields are marked *

Go up