Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

OData JSON Deserialize

srikanthnalluri
Active Participant

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

1 ACCEPTED SOLUTION

alexey_arseniev
Advisor
Advisor
0 Kudos

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.

10 REPLIES 10

geert-janklaps
Active Contributor

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

Not an XML. See the JSON in the image that the OP has attached.

0 Kudos

Hi sandra.rossi,

My mistake, that’s what you get wegen trying to multitask. I updated the answer!

Best regards,

Geert-Jan Klaps

0 Kudos

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?

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

0 Kudos

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.

Sandra_Rossi
Active Contributor
0 Kudos

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.

srikanthnalluri
Active Participant
0 Kudos
{"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

alexey_arseniev
Advisor
Advisor
0 Kudos

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.

0 Kudos

Thanks Alexey, your blog helped me a lot