Converting String to JSON in SAP ABAP
In SAP ABAP, converting a string into a JSON object typically involves using /UI2/CL_JSON
or CALL TRANSFORMATION
methods. Below are different approaches.
1. Using /UI2/CL_JSON
(Recommended for Standard Cases)
The class /UI2/CL_JSON
helps convert a JSON string into an ABAP structure or internal table.
Example: Convert JSON String to ABAP Structure
DATA: lv_json TYPE string, ls_data TYPE ty_json_data. " Define your structure
lv_json = '{ "name": "SAP", "year": 2025 }'.
TRY. /ui2/cl_json=>deserialize( EXPORTING json = lv_json CHANGING data = ls_data ). WRITE: 'Name:', ls_data-name, 'Year:', ls_data-year. CATCH cx_root INTO DATA(lx_error). WRITE: 'JSON Parsing Error:', lx_error->get_text( ).ENDTRY.
✅ Converts JSON to an ABAP structure ❌ Requires structure definition before parsing
2. Using CALL TRANSFORMATION
(For XML-like JSON Parsing)
You can also use CALL TRANSFORMATION
to parse JSON into ABAP.
Example: Convert JSON to ABAP Structure
DATA: lv_json TYPE string VALUE '{ "product": "ABAP", "version": "7.5" }', ls_data TYPE ty_json_data.
CALL TRANSFORMATION id SOURCE XML lv_json RESULT ls_data.
WRITE: ls_data-product, ls_data-version.
✅ Works well for simple JSON ❌ Limited when dealing with nested structures
3. Converting JSON String to Internal Table
If your JSON contains multiple entries, convert it into an internal table.
Example: Convert JSON Array to Internal Table
DATA: lv_json TYPE string, lt_table TYPE TABLE OF ty_json_table.
lv_json = '[{ "id": 1, "name": "Alice" }, { "id": 2, "name": "Bob" }]'.
/ui2/cl_json=>deserialize( EXPORTING json = lv_json CHANGING data = lt_table).
LOOP AT lt_table INTO DATA(ls_row). WRITE: / ls_row-id, ls_row-name.ENDLOOP.
✅ Parses JSON arrays into an internal table ❌ Requires correct field structure
4. Handling Nested JSON Objects
For deeply nested JSON, define nested structures.
Example: Handling Nested JSON
TYPES: BEGIN OF ty_address, city TYPE string, zip TYPE string, END OF ty_address.
TYPES: BEGIN OF ty_person, name TYPE string, age TYPE i, address TYPE ty_address, END OF ty_person.
DATA: lv_json TYPE string VALUE '{ "name": "John", "age": 30, "address": { "city": "Berlin", "zip": "10117" } }', ls_person TYPE ty_person.
/ui2/cl_json=>deserialize( EXPORTING json = lv_json CHANGING data = ls_person).
WRITE: ls_person-name, ls_person-age, ls_person-address-city, ls_person-address-zip.
✅ Works for nested JSON structures ❌ Needs manual structure definition
5. Handling Errors in JSON Parsing
Use exception handling to catch JSON errors.
TRY. /ui2/cl_json=>deserialize( EXPORTING json = lv_json CHANGING data = ls_data ). CATCH cx_root INTO DATA(lx_error). WRITE: 'JSON Parsing Error:', lx_error->get_text( ).ENDTRY.
✅ Ensures error handling during JSON conversion
🚀 Summary: Best Methods for Converting String to JSON
Method | Use Case |
---|---|
/UI2/CL_JSON=>deserialize | Best for simple & nested JSON |
CALL TRANSFORMATION id | Works for basic JSON |
TABLE OF ty_json_table | Converts JSON arrays |
Nested Structures | Parses complex JSON objects |
TRY...CATCH | Handles errors in JSON parsing |