Application Development and Automation 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: 
Read only

Need to Convert json to abap structure but having problems

Former Member
0 Likes
4,184

So I am doing a get request to a webservice and getting from that a json.

The problem is that I am not able to convert it and I have tried three ways but all failed.

Try 1

I used the method cl_fdt_json=>json_to_data

call method cl_fdt_json=>json_to_data
        exporting
          iv_json = gv_response
        changing
          ca_data =  gt_data_json.

And the table in output remains initial .

Try 2

data gr_json_deserializer type ref to cl_trex_json_deserializer
create object gr_json_deserializer.
    call method gr_json_deserializer->deserialize
     exporting
        json = gv_response
      importing
        abap = gt_data_json.

Try 3

CALL TRANSFORMATION id
           SOURCE XML gv_response
           RESULT data = gt_data_json.

None of this is working .

Here is the json that has to be converted:

json.txt

This is the structure I used to handle this.

types: begin of zstockblock,
           qty_ordered type i,
           sku         type string,
           end of zstockblock.


    types: begin of z_stockblock_v,
           entity_id type i,
           items     type zstockblock,
           total_count type i,
          end of z_stockblock_v.
data gt_data_json    type table of z_stockblock_v

Don't look at the data type since I was going to change them if it was going to work

Can anyone help me ? I am stuck

So I am doing a get request to a webservice and getting from that a json.

The problem is that I am not able to convert it and I have tried three ways but all failed.

Try 1

I used the method cl_fdt_json=>json_to_data

call method cl_fdt_json=>json_to_data
        exporting
          iv_json = gv_response
        changing
          ca_data =  gt_data_json.

And the table in output remains initial .

Try 2

data gr_json_deserializer type ref to cl_trex_json_deserializer
create object gr_json_deserializer.
    call method gr_json_deserializer->deserialize
     exporting
        json = gv_response
      importing
        abap = gt_data_json.

Try 3

CALL TRANSFORMATION id
           SOURCE XML gv_response
           RESULT data = gt_data_json.

None of this is working .

Here is the json that has to be converted:

json.txt

This is the structure I used to handle this.

types: begin of zstockblock,
           qty_ordered type i,
           sku         type string,
           end of zstockblock.


    types: begin of z_stockblock_v,
           entity_id type i,
           items     type zstockblock,
           total_count type i,
          end of z_stockblock_v.
data gt_data_json    type table of z_stockblock_v

Don't look at the data type since I was going to change them if it was going to work

Can anyone help me ? I am stuck

3 REPLIES 3
Read only

former_member593648
Active Participant
0 Likes
2,358

Hi,

The CL_FDT_CLASS will work. There needs to be a little conversion before you pass values into the class.Check this link

https://answers.sap.com/questions/383255/how-to-convert-json-response-from-rest-api-to-abap.html

Read only

0 Likes
2,358

Which little conversion exactly? I can not see it in the URL

-- Tomas --
Read only

former_member593648
Active Participant
0 Likes
2,358

Hi Tomas,

Check this code segment..this works just fine for me.


TYPES : BEGIN OF str_json,

        success(20),

        msg(20),

        matnr(20),

        matkl(20),

        END OF str_json.

DATA : response TYPE string VALUE '{"success":"false","msg":"Not valid","matnr":"1234","matkl":"123489"}',

     lt_data_json TYPE str_json.


CALL METHOD cl_fdt_json=>json_to_data

  EXPORTING

    iv_json = response

  CHANGING

    ca_data = lt_data_json.