jsonabapspace

Deserialize JSON with field names containing spaces?


I am getting JSON response like below

[ { "Status": "00" }, { "Order Due Date": "", "Customer Address": "", "ConsumerId": "", "Amount Paid": "", "Date Created": "", "Customer Email": "", "TransactionStatus": "", "Date Paid": "", "Total Penalty": "", "OrderId": "", "Payment Mode": "", "Id": "", "Issue Date": "", "Customer Mobile": "", "Order Amount": "" } ]

As can be seen, field names have spaces in them. I am deserializing it via /ui2/cl_json=>deserialize into the following table, but it's not happening due to the spaces in field names.

TYPES: BEGIN OF ty_data,
         Id             TYPE string,
         "Order Due Date" TYPE string, "invalid ABAP syntax, commented
         OrderDueDate   TYPE string, "correct
         fld3           TYPE string,
         fld4           TYPE string,
         fld5           TYPE string,
         fld6           TYPE string,
         fld7           TYPE string,
         fld8           TYPE string,
         fld9           TYPE string,
         fld10          TYPE string,
         fld11          TYPE string,
         fld12          TYPE string,
         fld13          TYPE string,
         fld14          TYPE string,
         fld15          TYPE string,
       END OF ty_data.

Please help. I am using ABAP 7.5.


Solution

  • The field names have to be mapped. This can be achived by using the name_mapping parameter of the method.

    JSON: Order Due Date

    ABAP: ORDER_DUE_DATE

    Mapping:

    DATA( lt_name_mappings ) = VALUE /ui2/cl_json=>name_mappings( ( abap = 'ORDER_DUE_DATE'
                                                            json = 'Order Due Date' ) ). "This for all fields to be mapped
    
    CALL METHOD /ui2/cl_json=>deserialize
      EXPORTING
        json          = json_file
    *   jsonx         =
    *   pretty_name   = PRETTY_MODE-NONE
    *   assoc_arrays  = C_BOOL-FALSE
    *   assoc_arrays_opt = C_BOOL-FALSE
        name_mappings = lt_name_mappings
    *   conversion_exits = C_BOOL-FALSE
    *   hex_as_base64 = C_BOOL-TRUE
      CHANGING
        data          = ls_data.