cancel
Showing results for 
Search instead for 
Did you mean: 

Convert the JSON to an ABAP structure

nandini_borse
Participant
0 Kudos
10,817

Hi All,

I'm trying to convert a JSON String into ABAP structure using 'deserialize' method of class 'cl_trex_json_deserializer'

Here is the JSON String which I want to convert into ABAP structure

json_response = {"access_token":"sometokenstring","token_type":"sometokentypestring","expires_in":"somekeystring","created_at": "somestring"}.

I have written code and I am getting the below dump.

2021-03-17.png (722×459)

and below is the code.

Could you help me to fix this. Its urgent

TYPES: BEGIN OF ty_start,
         access_token   TYPE string,
         token_type     TYPE string,
         expires_in  TYPE D,
         created_at TYPE T,
       END OF ty_start.

TYPES: BEGIN OF ty_fin,
         access_token TYPE string,
         token_type   TYPE string,
         expires_in   TYPE string,
         created_at   TYPE string,
       END OF ty_fin.

DATA ls_start TYPE ty_start.
DATA ls_fin   TYPE ty_fin.
DATA lv_json  TYPE string.

DATA(json_response) = lo_response->get_string_data( ).
DATA lr_json_serializer   TYPE REF TO cl_trex_json_serializer.
DATA lr_json_deserializer TYPE REF TO cl_trex_json_deserializer.

CREATE OBJECT lr_json_serializer
  EXPORTING
    data = json_response.
lr_json_serializer->serialize( ).
lv_json = lr_json_serializer->get_data( ).


CREATE OBJECT lr_json_deserializer.
lr_json_deserializer->deserialize(
EXPORTING
  json   = lv_json
IMPORTING
  abap   = ls_fin ).

View Entire Topic
nandini_borse
Participant
Solved!!.. its due to nested deep structure.. here is my code

data :u_id      type string,
          emp_id    type string,
          ls_everfi type ty_everfi,
          lr_data   type ref to data,
          lt_act    type sorted table of ty_everfi with unique key employee_id.

    lr_data = /ui2/cl_json=>generate( json = json_response_send ).
    /ui2/cl_data_access=>create( ir_data = lr_data iv_component = `data-id`)->value( importing ev_data = u_id ).
    /ui2/cl_data_access=>create( ir_data = lr_data iv_component = 'data-attributes-employee_id')->value( importing ev_data = emp_id ).

chrwittm
Advisor
Advisor
0 Kudos
Thanks a lot, finally there is a way to map a JSON to ABAP without static types!