Srinivas chandu
Srinivas chandu

Reputation: 7

Count rows in json using /UI2/CL_JSON

i have a json value stored in a variable now i need to get the count of rows and i am facing error.

when i am storing the deserilized data in ls_response it shows error as

ls_Response is not an internal table

when i am storing the deserilized data in lt_response it show output as

total count is 0

here is the code

  CLASS z_http1_code DEFINITION
      PUBLIC
      FINAL
      CREATE PUBLIC .
    
    PUBLIC SECTION.
    
        INTERFACES if_oo_adt_classrun .
    
        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,
        tt_field type standard table 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.
    
        DATA:ls_Response TYPE  ty_response,
             lt_response type tt_record.
    
        TYPES: BEGIN OF ty_serialize,
             customer_id  TYPE string,
             customer     TYPE string,
           END OF ty_serialize.
    
DATA(lv_response) = `{"records":[{"id":"rec5Qk24OQpKDyykq","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010001","address":"Chennai","time_created":"06:00:14","customer":"IDADMIN","date_created":"16.04.2004"}},{"id":"rec7bSe8` &&
  `Zb18z6b5a","createdTime":"2022-08-08T13:07:16.000Z","fields":{"customer_id":"0000010007","address":"Kakinada","time_created":"04:01:18","customer":"Ramya","date_created":"15.04.2000"}},{"id":"recD9Hh4YLgNXOhUE","createdTime":"2022-08-08T11:48:06.00` &&
  `0Z","fields":{"customer_id":"0000010002","address":"Bangalore","time_created":"04:03:35","customer":"MAASSBERG","date_created":"20.04.2004"}},{"id":"recK7Tfw4PFAedDiB","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010005","a` &&
  `ddress":"Kakinada","time_created":"12:55","customer":"Lakshmi","date_created":"13-10-2022"}},{"id":"recKOq0DhEtAma7BV","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010006","address":"Hyderabad","time_created":"18:42:28","cu` &&
  `stomer":"GLAESS","date_created":"21.04.2004"}},{"id":"recS8pg10dFBGj8o7","createdTime":"2022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010003","address":"Gurugram","time_created":"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","time_created":"06:00:12","customer":"IDADMIN","date_created":"21.04.2004"}},{"id":"recs7oHEqfkN87tWm","createdTime":"2` &&
  `022-08-03T10:14:43.000Z","fields":{"customer_id":"0000010000","address":"Hyderabad","time_created":"04:01:18","customer":"MAASSBERG","date_created":"15.04.2004"}}]}`.

 PROTECTED SECTION.
      PRIVATE SECTION.
    ENDCLASS.

  /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             = lt_response
    ).

Data LV_RowCnt type i.

*LV_RowCnt = LINES( Lt_response ).
*out->write( |'Total items count' { LV_RowCnt }| ).

Upvotes: 0

Views: 88

Answers (1)

Stefan
Stefan

Reputation: 241

You need to store the deserialized data in ls_response. After that, you can get the row count of the field records of the structure ls_response:

DATA lv_row_count type i.
lv_row_count = LINES( ls_response-records ).

Upvotes: 2

Related Questions