To convert JSON to an ABAP object, you can use the following code. The pretty_name
parameter should be set according to the naming convention of the fields in the JSON.
Input:
[
{
"id": 32,
"name": "John",
"lastName": "Smith",
"isActive": true,
"department": "IT"
},
{
"id": 22,
"name": "Emily",
"lastName": "Johnson",
"isActive": false
}
]
types: begin of ts_employee,
id type i,
name type string,
last_name type string,
is_active type abap_bool,
department type string,
end of ts_employee.
data : lt_employees type standard table of ts_employee.
*lv_json is mentioned above.
/ui2/cl_json=>deserialize(
exporting
json = lv_json
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
changing
data = lt_employees
).
If the JSON contains spaces or characters that cannot be defined as fields in ABAP, the name_mappings
parameter should be used for this purpose.
Input:
[
{
"id": 32,
"name": "John",
"Last Name": "Smith",
"isActive": true
},
{
"id": 22,
"name": "Emily",
"Last Name": "Johnson",
"isActive": false
}
]
data : lt_mappings type /ui2/cl_json=>name_mappings,
ls_mapping like line of lt_mappings.
ls_mapping-abap = 'last_name'.
ls_mapping-json = 'Last Name'.
append ls_mapping to lt_mappings.
clear ls_mapping.
/ui2/cl_json=>deserialize(
exporting
json = lv_json
pretty_name = /ui2/cl_json=>pretty_mode-camel_case
name_mappings = lt_mappings
changing
data = lt_employees
).