Skip to Content

OData JSON Deserialize

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

odata.png (19.0 kB)
Add comment
10|10000 characters needed characters exceeded

  • 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.

  • {"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

2 Answers

  • Best Answer
    Posted on Mar 13 at 05: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.

    Add comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 09 at 09: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

    Add comment
    10|10000 characters needed characters exceeded

    • Thanks Geert-Jan Klaps, 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.