Home > Mobile >  convert JSON to CSV in abap
convert JSON to CSV in abap

Time:11-03

I need to the following JSON to csv in abap adt.

 data(rep) = {
        "name": "John",
        "age": "22",
        "gender": "male",
    
    }
    {
        "name": "ram",
        "age": "21",
        "gender": "male",
    
    }
    {
        "name": "Janu",
        "age": "22",
        "gender": "female",
    
    }

which function is used for to convert csv.

CodePudding user response:

I would recommend you to first save the JSON string to an internal table and then create a CSV from the internal table.

TYPES: BEGIN OF t_person,
         name  TYPE string,
         age TYPE i,
         gender TYPE string,
       END OF t_person.

TYPES: tt_person TYPE STANDARD TABLE OF t_person WITH DEFAULT KEY.

DATA: lt_csv TYPE truxs_t_text_data.

DATA: json TYPE string VALUE '{"VALUES":[{
        "name": "John",
        "age": "22",
        "gender": "male",
    
    },
    {
        "name": "ram",
        "age": "21",
        "gender": "male",
    },
    {
        "name": "Janu",
        "age": "22",
        "gender": "female",
    }]}'.

DATA(lt_persons) = VALUE tt_person( ).

"Convert to internal table
CALL TRANSFORMATION id SOURCE XML json RESULT values = lt_persons.

"Convert to CSV
CALL FUNCTION 'SAP_CONVERT_TO_TEX_FORMAT'
    EXPORTING
      i_field_seperator    = ';'
    TABLES
      i_tab_sap_data       = lt_persons
    CHANGING
      i_tab_converted_data = lt_csv
    EXCEPTIONS
      conversion_failed    = 1
      OTHERS               = 2.
  • Related