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

Json to abap coversion not happening

0 Kudos
1,939

Hello All,

I am using mthod cl_fdt_json=>json_to_data to convert my json response to abap structure but i am not getting data back in abap structure . below is the json response and the abap structure. please help to identify the issue? its urgent.

{
   "destination_addresses" : [ "Zeppelinstraße 100, 73730 Esslingen am Neckar, Germany" ],
   "origin_addresses" : [ "Auf d. Rut 4, 64668 Rimbach, Germany" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "171 km",
                  "value" : 171307
               },
               "duration" : {
                  "text" : "2 hours 6 mins",
                  "value" : 7581
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

1 ACCEPTED SOLUTION
Read only

Sandra_Rossi
Active Contributor
1,750

In JSON, square brackets mean tables, but you defined structures in ABAP instead of table types...

NB: I agree with Manfred that you'd better create "a self contained test program which takes the JSON string as constant input and then trying the remaining decoding." It's also the way I troubleshoot issues and this way it's more easy to ask people to look at my issue if I can't find a solution myself.

6 REPLIES 6
Read only

manfred_reinart
Product and Topic Expert
Product and Topic Expert
0 Kudos
1,750

Would be great if you also provided the ABAP type part and the API call portion as source code of a self contained test program rather than bitmaps.

Read only

0 Kudos
1,750

hi manfred.reinart ,

here is my code and i haven't created abap type yet, will do it after getting the conversion in my dummy structre.

CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = api_url
IMPORTING
client = lo_http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.

IF sy-subrc <> 0.
MESSAGE 'Error creating client' TYPE 'E'.
ENDIF.


CREATE OBJECT lo_rest_client
EXPORTING
io_http_client = lo_http_client.

lo_http_client->request->set_version( if_http_request=>co_protocol_version_1_1 ).

*Set Payload or body ( JSON or XML)
lo_request = lo_rest_client->if_rest_client~create_request_entity( ).
lo_request->set_content_type( iv_media_type = if_rest_media_type=>gc_appl_json ).
lo_request->set_string_data( lv_body ).

CALL METHOD lo_rest_client->if_rest_resource~post
EXPORTING
io_entity = lo_request.

lo_response = lo_rest_client->if_rest_client~get_response_entity( ).
http_status = lv_status = lo_response->get_header_field( '~status_code' ).
reason = lo_response->get_header_field( '~status_reason' ).
content_length = lo_response->get_header_field( 'content-length' ).
location = lo_response->get_header_field( 'location' ).
content_type = lo_response->get_header_field( 'content-type' ).
response = lo_response->get_string_data( ).


TYPES: BEGIN OF response_abap,
destination_addresses TYPE string,
origin_addresses TYPE string,
BEGIN OF rows,
BEGIN OF elements,
BEGIN OF distance,
text TYPE string,
value TYPE string,
END OF distance,
BEGIN OF duration,
text TYPE string,
value TYPE string,
END OF duration,
status TYPE string,
END OF elements,
END OF rows,
status TYPE string,
END OF response_abap.

DATA : dummy_struc TYPE response_abap.

CALL METHOD cl_fdt_json=>json_to_data
EXPORTING
iv_json = response
CHANGING

ca_data = dummy_struc.

Read only

manfred_reinart
Product and Topic Expert
Product and Topic Expert
1,750

you should sort out such issues by creating a self contained test program which takes the JSON string as constant input and then trying the remaining decoding.
This is then easier to reproduce than to rely on the HTTP communication upfront.

Have you also thought about checking on CALL TRANSFORMATION 'ID'?

Read only

Sandra_Rossi
Active Contributor
1,751

In JSON, square brackets mean tables, but you defined structures in ABAP instead of table types...

NB: I agree with Manfred that you'd better create "a self contained test program which takes the JSON string as constant input and then trying the remaining decoding." It's also the way I troubleshoot issues and this way it's more easy to ask people to look at my issue if I can't find a solution myself.

Read only

0 Kudos
1,750

Hi , while converting to ABAP structure, need to be careful while defining the exact matching structure with that of Json structure. Check that.

Read only

0 Kudos
1,750

Thanku all for information. I am able to solve it by declaring the structure in the correct way and using /ui2/cl_json=>deserialize Method.