03-09-2020 9:08 PM
Hi All,
I am trying to deserialize the simple JSON data using the class /UI2/CL_JSON. However the output internal table is initial. Can you please let me know what i am missing here.
types:
begin of ty_ekko,
ebeln type ebeln,
bukrs type bukrs,
bstyp type bstyp,
ktwrt type string,
end of ty_ekko.
data:
lt_ekko type STANDARD TABLE OF ty_ekko.
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_json
CHANGING
data = lt_ekko ).
check lt_ekko is not initial.
We're on SAP_ABA - 740 SP-Level - 0009
Thank you
03-13-2020 5:50 PM
Hello S Nalluri,
you are using the wrong/incomplete receiving ABAP structure, that is why it does not work.
Try this one:
types:
begin of ty_ekko,
ebeln type ebeln,
bukrs type bukrs,
bstyp type bstyp,
ktwrt type string,
end of ty_ekko,
begin of ty_odata_results,
results type standard table of ty_ekko,
end of ty_odata_results,
begin of ty_odata,
d type ty_odata_results,
end of ty_odata.
data:
ls_odata type ty_odata.
/ui2/cl_json=>deserialize( EXPORTING json = lv_json
CHANGING data = ls_odata ).
check ls_odata is not initial.
You also do not need to use the GENERATE method. It is only useful in case you do not know the JSON structure.
BR, Alexey.
03-09-2020 9:13 PM
Hi,
You can't always assign the internal table to the changing parameter data. This depends on the structure of your JSON file. A full guide on how to use the class /ui2/cl_json to deserialize a JSON string can be found in this wiki: https://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer
Best regards,
Geert-Jan Klaps
03-10-2020 12:23 AM
03-10-2020 4:56 AM
Hi sandra.rossi,
My mistake, that’s what you get wegen trying to multitask. I updated the answer!
Best regards,
Geert-Jan Klaps
03-12-2020 7:52 PM
8d8214c7f9734f45be69f95cc0d5aeee Thanks for the link, It seems there is a method - GENERATE which can be used but i don't have this method.In this case do i have to build the internal table structure using JSON structure?
03-12-2020 8:52 PM
Hi srikanthnalluri,
You should use /UI2/CL_DATA_ACCESS to read the data dynamically. Based on your example, I made this minimal program:
DATA lv_json TYPE string.
lv_json = `{"d":{"results":[{"__metadata":{"id":"http://xxxxxx.com:8000/sap/opu/odata/SAP/ZSN_TEST_REST_SERVICE_SRV/POHeaderDataSet('4100353003')","uri":"http://xxxxxx.com:8000/sap/opu/odata/SAP/ZSN_TEST_REST_SERVICE_SRV/POHeaderDataSet('4100353003')",`
&& `"type":"ZSN_TEST_REST_SERVICE_SRV.POHeaderData"},"Ebeln":"4100353003","Bukrs":"0010","Bstyp":"F","Bsart":"NB","Ktwrt":"0.000"},{"__metadata":{"id":"http://xxxxxx.com:8000/sap/opu/odata/SAP/ZSN_TEST_REST_SERVICE_SRV/POHeaderDataSet('4100353103')",`
&& `"uri":"http://xxxxxx.com:8000/sap/opu/odata/SAP/ZSN_TEST_REST_SERVICE_SRV/POHeaderDataSet('4100353103')","type":"ZSN_TEST_REST_SERVICE_SRV.POHeaderData"},"Ebeln":"4100353103","Bukrs":"0010","Bstyp":"F","Bsart":"NB","Ktwrt":"0.000"}]}}`.
TYPES:
BEGIN OF ty_ekko,
ebeln TYPE ebeln,
bukrs TYPE bukrs,
bstyp TYPE bstyp,
ktwrt TYPE string,
END OF ty_ekko.
DATA: lr_data TYPE REF TO data,
lt_ekko TYPE STANDARD TABLE OF ty_ekko,
ls_ekko LIKE LINE OF lt_ekko.
FIELD-SYMBOLS: <ft_result> TYPE ANY TABLE,
<fs_data_ref> TYPE any,
<fs_structure> TYPE any,
<fs_field> TYPE any,
<fs_value> TYPE any.
* Convert JSON to data reference
/ui2/cl_json=>deserialize(
EXPORTING
json = lv_json
CHANGING
data = lr_data ).
* Prepare data access
DATA(lr_result_base) = /ui2/cl_data_access=>create( ir_data = lr_data iv_component = `D`)->ref( ).
DATA(lr_result) = /ui2/cl_data_access=>create( ir_data = lr_result_base iv_component = `RESULTS`)->ref( ).
* Assign result table to field symbol
ASSIGN lr_result->* TO <ft_result>.
* Loop at result table
LOOP AT <ft_result> ASSIGNING <fs_data_ref>.
* Assign data reference to field symbol
ASSIGN <fs_data_ref>->* TO <fs_structure>.
IF sy-subrc = 0.
* Read purchase order
ASSIGN COMPONENT 'EBELN' OF STRUCTURE <fs_structure> TO <fs_field>.
IF sy-subrc = 0.
ASSIGN <fs_field>->* TO <fs_value>.
IF sy-subrc = 0.
ls_ekko-ebeln = <fs_value>.
ENDIF.
ENDIF.
* Add line to internal table
APPEND ls_ekko TO lt_ekko.
CLEAR: ls_ekko.
ENDIF.
ENDLOOP.<br>
The "Read purchase order" part needs to be repeated for each field in your structure. (you can do this dynamically off course so you don't need to repeat the code)
Result:
Best regards,
Geert-Jan Klaps
03-12-2020 9:20 PM
Thanks 8d8214c7f9734f45be69f95cc0d5aeee, unfortunately we don't have the class - /UI2/CL_DATA_ACCESS and the changing parameter in the deserialize method never returns anything in my case . However i used the class-ZCL_JSON which is provided in the link, and it does have generate method.
03-10-2020 12:22 AM
You'd better post your JSON as code instead of image, because usually people won't spend time in retyping the JSON from the image to reproduce the issue and propose a solution. Sorry.
03-10-2020 3:43 PM
{"d":{"results":[{"__metadata":{"id":"http://xxxxxx.com:8000/sap/opu/odata/SAP/ZSN_TEST_REST_SERVICE_SRV/POHeaderDataSet('4100353003')","uri":"http://xxxxxx.com:8000/sap/opu/odata/SAP/ZSN_TEST_REST_SERVICE_SRV/POHeaderDataSet('4100353003')","type":"ZSN_TEST_REST_SERVICE_SRV.POHeaderData"},"Ebeln":"4100353003","Bukrs":"0010","Bstyp":"F","Bsart":"NB","Ktwrt":"0.000"},{"__metadata":{"id":"http://xxxxxx.com:8000/sap/opu/odata/SAP/ZSN_TEST_REST_SERVICE_SRV/POHeaderDataSet('4100353103')","uri":"http://xxxxxx.com:8000/sap/opu/odata/SAP/ZSN_TEST_REST_SERVICE_SRV/POHeaderDataSet('4100353103')","type":"ZSN_TEST_REST_SERVICE_SRV.POHeaderData"},"Ebeln":"4100353103","Bukrs":"0010","Bstyp":"F","Bsart":"NB","Ktwrt":"0.000"}]}}
JSON file
03-13-2020 5:50 PM
Hello S Nalluri,
you are using the wrong/incomplete receiving ABAP structure, that is why it does not work.
Try this one:
types:
begin of ty_ekko,
ebeln type ebeln,
bukrs type bukrs,
bstyp type bstyp,
ktwrt type string,
end of ty_ekko,
begin of ty_odata_results,
results type standard table of ty_ekko,
end of ty_odata_results,
begin of ty_odata,
d type ty_odata_results,
end of ty_odata.
data:
ls_odata type ty_odata.
/ui2/cl_json=>deserialize( EXPORTING json = lv_json
CHANGING data = ls_odata ).
check ls_odata is not initial.
You also do not need to use the GENERATE method. It is only useful in case you do not know the JSON structure.
BR, Alexey.
03-17-2020 5:44 PM