Search code examples
jsoneclipseparsingabapjson-deserialization

Parsing json using deserialize in abap btp environment


I fetched the data from a URL and data is stored in a variable in json format.now i need to parse this json format but i am unable to parse the data. below is the code

CLASS zcode_82 DEFINITION

  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun .
    DATA:lv_response TYPE string,
         lv_body type string,
         lv_path type string,
           lv_json     TYPE /ui2/cl_json=>json,
         r_json type string.

types: begin of ty_data,
       field type string,
       id type string,
       customer type string,
       customer_id type string,
       address type string,
       date_Created type string,
       time_created type string,
       END OF TY_DATA.



data: lv_data type STANDARD TABLE OF ty_data with DEFAULT KEY,
       lr_data    TYPE REF TO data,
      ls_data type ty_data.

  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.


CLASS zcode_82 IMPLEMENTATION.

  METHOD if_oo_adt_classrun~main.
    DATA: lv_body_1 TYPE string,
          ls_value type RANGE OF ty_data.
    TRY.
        "create http destination by url; API endpoint for API sandbox
        DATA(lo_http_destination) =
             cl_http_destination_provider=>create_by_url( 'enter the url ' ).



        "create HTTP client by destination
        DATA(lo_web_http_client) = cl_web_http_client_manager=>create_by_http_destination( lo_http_destination ) .
        "adding headers with API Key for API Sandbox
        DATA(lo_web_http_request) = lo_web_http_client->get_http_request( ).
        lo_web_http_request->set_header_fields( VALUE #(
        (  name = 'Authorization' value = 'Bearer key' )
        (  name = 'Content-Type' value = 'application/json' )
        ) ).
        "set request method and execute request
        DATA(lo_web_http_response) = lo_web_http_client->execute( if_web_http_client=>get ).
        lv_response = lo_web_http_response->get_text( ).
      CATCH cx_http_dest_provider_error cx_web_http_client_error cx_web_message_error.
        "error handling
    ENDTRY.

*  out->write( |response:  { lv_response }| ).
*
CLEAR lv_data[].

/ui2/cl_json=>deserialize(
  EXPORTING
    json             = lv_Response
*    jsonx            =
    pretty_name      = /ui2/cl_json=>pretty_mode-user
*    assoc_arrays     =
*    assoc_arrays_opt =
*    name_mappings    =
*    conversion_exits =
*    hex_as_base64    =
  CHANGING
    data             = lv_data
).
out->write(
  EXPORTING
    data   = lv_data
*    name   =
*  RECEIVING
*    output =
).
endmethod.
endclass.

Here the downloaded data from url is the input and the input is

response:  {"records":[{"id":"rec5Qk24OQpKDyykq","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010001","address":"Chennai","created_time":"06:00:14","customer":"IDADMIN","date_created":"16.04.2004"}},{"id":"rec7bSe8Zb18z6b5a","createdTime":"2022-08-08T13:07:16.000Z","fields":{"customer_id":"0000010007","address":"Kakinada","created_time":"04:01:18","customer":"Ramya","date_created":"15.04.2000"}},{"id":"recD9Hh4YLgNXOhUE","createdTime":"2022-08-08T11:48:06.000Z","fields":{"customer_id":"0000010002","address":"Bangalore","created_time":"04:03:35","customer":"MAASSBERG","date_created":"20.04.2004"}},{"id":"recK7Tfw4PFAedDiB","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010005","address":"Chennai","created_time":"06:00:49","customer":"IDADMIN","date_created":"21.04.2004"}},{"id":"recKOq0DhEtAma7BV","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010006","address":"Hyderabad","created_time":"18:42:28","customer":"GLAESS","date_created":"21.04.2004"}},{"id":"recS8pg10dFBGj8o7","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010003","address":"Gurugram","created_time":"04:10:02","customer":"MAASSBERG","date_created":"20.04.2004"}},{"id":"recf4QbOmKMrBeLQZ","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010004","address":"Bangalore","created_time":"06:00:12","customer":"IDADMIN","date_created":"21.04.2004"}},{"id":"recs7oHEqfkN87`enter code here`tWm","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010000","address":"Hyderabad","created_time":"04:01:18","customer":"MAASSBERG","date_created":"15.04.2004"}}]}

The output of the code is

Table  
FIELD  ID  CUSTOMER  CUSTOMER_ID  ADDRESS  DATE_CREATED  TIME_CREATED  

Solution

  • Your structure type simply does not match the JSON structure. You can't simply skip the outer records array and expect the deserializer to know you want to deserialize what is within it. Same with the fields object to string, it's an object/structure, not a string.

    The JSON <> ABAP type would look like this

    {
       "records":[
          {
             "id":"rec5Qk24OQpKDyykq",
             "createdTime":"2022-08-03T10:14:43.000Z",
             "fields":{
                "customer_id":"0000010001",
                "address":"Chennai",
                "created_time":"06:00:14",
                "customer":"IDADMIN",
                "date_created":"16.04.2004"
             }
          },
          ...
    
    TYPES: BEGIN OF ty_field,
             customer_id TYPE string,
             address TYPE string,
             created_time TYPE string,
             customer TYPE string,
             date_created TYPE string,
           END OF ty_field.
    TYPES: BEGIN OF ty_record,
             id TYPE string,
             createdtime TYPE string,
             fields TYPE ty_field,
           END OF ty_record.
    TYPES tt_record TYPE STANDARD TABLE OF ty_record WITH EMPTY KEY.
    TYPES: BEGIN OF ty_response,
             records TYPE tt_record,
           END OF ty_response.
    

    Then you can use a variable like DATA ls_response TYPE ty_response as your data parameter.

    Side note: If you can, consider using Simple Transformations with JSON data. There you can fine-tune namings (mix snake_case and camelCase), which fields to serialize, deserialze, require/optional, and it's magnitudes faster (especially with larger json files).