cancel
Showing results for 
Search instead for 
Did you mean: 

how to convert JSON to ABAP

SenthilVelavan_19
Discoverer
0 Kudos
159

This is my JSON response,

{ "status": 500 ,
"reason": " Internal Server Error ",
"response": " {"status":500,
"payload":{"errorType":"Error",
"errorMessage":"{\"statusCode\":404,
\"status\":\"Error Halo Cancel request failed for notification no: 85004240, inspection id: mqp4_09062023_1: Request failed with status code 404\",
\"body\":\"Error Halo Cancel request failed for notification no: 85004240, inspection id: mqp4_09062023_1\"}",
"trace":["Error: {\"statusCode\":404,
\"status\":\"Error Halo Cancel request failed for notification no: 85004240, inspection id: mqp4_09062023_1: Request failed with status code 404\",
\"body\":\"Error Halo Cancel request failed for notification no: 85004240, inspection id: mqp4_09062023_1\"}",
" at respondByThrowingError (/var/task/src/index.js:34:9)",
" at Runtime.exports.handler (/var/task/src/index.js:71:7)",
" at processTicksAndRejections (node:internal/process/task_queues:96:5)"]
}
} "
}

How to convert this into internal table or ABAP perspective?

Tried Deserialize method but not working.

Can anyone tell or suggest?

Accepted Solutions (1)

Accepted Solutions (1)

Sandra_Rossi
Active Contributor

As you have a short dump in a standard program (/UI2/CL_JSON in your case), check the SAP notes or contact the SAP support. Whenever you request some assistance about a short dump, you have to attach the short dump, as a text file, along with minimal reproducible ABAP code, otherwise you won't get a fast answer.

I was starting to test your code in my ABAP 7.58 system, but the JSON has an invalid syntax. Why is it incorrect? If you obtain it by debug, you may just copy and paste it in your question to avoid a manual-typing error.

Anyway, I ran your code and there's no exception in 7.58, but it's possible that /UI2/CL_JSON fail in some other versions do raise a class-based exception CX_SY_MOVE_CAST_ERROR, which becomes the runtime error MOVE_CAST_ERROR if you don't wrap the call inside a TRY CATCH ENDTRY block).

I did the following code which works as I expect. I found many errors in your JSON and in your code. You use camel case, so JSON "errorType" should correspond to the ERROR_TYPE ABAP component (same for others). You are using an internal table for a JSON Object (element within curly brackets), you should use a structure for a JSON Object, and an internal table for a JSON Array (element with square brackets).

In your JSON example, I also feel that your JSON Object "trace" should only contain strings and that you mistakenly inserted the JSON Object "Error" as the first array element.

 

TYPES:
*       BEGIN OF ty_error,
*         statuscode TYPE int4,
*         status     TYPE string,
*         body       TYPE string,
*       END OF ty_error,
*
*       BEGIN OF ty_trace,
*         error     TYPE ty_error,
*         lv_error  TYPE string,
*         lv_error1 TYPE string,
*         lv_error2 TYPE string,
*       END OF ty_trace,

       BEGIN OF ty_errormessage,
         status_code TYPE int4,
*         statuscode TYPE int4,
         status     TYPE string,
         body       TYPE string,
*         error TYPE ty_error,
       END OF ty_errormessage,

       BEGIN OF ty_payload,
         error_type    TYPE string,
         error_message TYPE ty_errormessage,
         trace         TYPE string_table,
*         errortype    TYPE string,
*         errormessage TYPE ty_errormessage,
*         trace        TYPE ty_trace,
       END OF ty_payload,

       BEGIN OF ty_response,
         status  TYPE int4,
         payload TYPE ty_payload,
       END OF ty_response.

TYPES: BEGIN OF ty_data,
         status TYPE int4,
         reason TYPE string,
         response TYPE ty_response,
*         response TYPE string,
       END OF ty_data.

DATA: lv_json        TYPE /ui2/cl_json=>json,
      lv_quote       TYPE char2,
      lv_quoteescape TYPE char2,
      ls_data        TYPE ty_data,
      lt_data        TYPE TABLE OF ty_data WITH HEADER LINE.

PARAMETERS: p_json TYPE string.

START-OF-SELECTION.

p_json = concat_lines_of( sep = |\n| table = value string_table(
( `{ "status": 500 ,` )
( `  "reason": " Internal Server Error ",` )
( `  "response": {  "status":500,` )
( `                 "payload":{` )
( `                     "errorType":"Error",` )
( `                     "errorMessage":{` )
( `                        "statusCode":404,` )
( `                        "status":"Error Halo Cancel request failed for notification no: 85004240, inspection id: mqp4_09062023_1: Request failed with status code 404",` )
( `                        "body":"Error Halo Cancel request failed for notification no: 85004240, inspection id: mqp4_09062023_1"` )
( `                        },` )
( `                     "trace":[` )
*( `                        "Error": {` )
*( `                            "statusCode":404,` )
*( `                            "status":"Error Halo Cancel request failed for notification no: 85004240, inspection id: mqp4_09062023_1: Request failed with status code 404",` )
*( `                            "body":"Error Halo Cancel request failed for notification no: 85004240, inspection id: mqp4_09062023_1"` )
*( `                            },` )
( `                         " at respondByThrowingError (/var/task/src/index.js:34:9)",` )
( `                         " at Runtime.exports.handler (/var/task/src/index.js:71:7)",` )
( `                         " at processTicksAndRejections (node:internal/process/task_queues:96:5)"` )
( `                        ]` )
( `                     }` )
( `                 }` )
( `}` ) ) ).
  IF p_json IS NOT INITIAL.
    lv_json = p_json.
    TRANSLATE lv_json TO LOWER CASE.
  ENDIF.

lv_json = p_json.

try.
 /ui2/cl_json=>deserialize(
    EXPORTING json             =  lv_json
              pretty_name      = /ui2/cl_json=>pretty_mode-camel_case
*              assoc_arrays     = /ui2/cl_json=>c_bool-true
*              assoc_arrays_opt = /ui2/cl_json=>c_bool-true
    CHANGING  data             = ls_data ).
catch cx_root into data(lx_root).
  " TODO
ENDTRY.

 

 

Answers (0)