[SAP ABAP] - JSON Parser

REPORT yjson_parser.

TYPES BEGIN OF ty_json,
          field1(50), "Field name harus sama dengan yg di json
          field2(50),
          field3(50),
          field4(50),
        END OF ty_json.

DATA : gi_json TYPE STANDARD TABLE OF ty_json WITH HEADER LINE.

DATA : gv_json TYPE string.

PARAMETERS: p_file(128) OBLIGATORY DEFAULT 'C:\'.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM fm_val_req_fname.

START-OF-SELECTION.

  PERFORM fm_load_file.
  PERFORM fm_read_json.

FORM fm_load_file.
  DATA : lv_filename  TYPE string.

  DATA : li_json LIKE STANDARD TABLE OF abaptxt255 WITH HEADER LINE.

  lv_filename = p_file.

  CALL FUNCTION 'GUI_UPLOAD'
    EXPORTING
      filename = lv_filename
    TABLES
      data_tab = li_json.

  LOOP AT li_json.
    CONCATENATE gv_json li_json-line INTO gv_json.
  ENDLOOP.

ENDFORM.

FORM fm_read_json.
  DATA : lv_data TYPE string.

  lv_data = gv_json.
  CONCATENATE '[' gv_json ']' INTO lv_data.

  CALL METHOD /ui2/cl_json=>deserialize
    EXPORTING
      json        = lv_data
      pretty_name = /ui2/cl_json=>pretty_mode-camel_case
    CHANGING
      data        = gi_json[].

  IF lines( gi_json ) = 0.
    MESSAGE 'Cannot read file !!' TYPE 'I'.
    EXIT.
  ENDIF.

  LOOP AT gi_json.
    WRITE : / gi_json-field1 COLOR 1, gi_json-field2, gi_json-field3.
  ENDLOOP.
ENDFORM.


*&---------------------------------------------------------------------*
*&      Form  fm_val_req_fname
*&---------------------------------------------------------------------*
FORM fm_val_req_fname .
  DATA: lv_program_name  LIKE sy-repid,
        lv_dynpro_number LIKE sy-dynnr,
        lv_field_name    LIKE dynpread-fieldname,
        lv_file_name     LIKE ibipparms-path.

  lv_program_name  = sy-repid.
  lv_dynpro_number = sy-dynnr.
  lv_field_name    = 'P_FNAME'.

  PERFORM fm_f4_filename
     USING    lv_program_name
              lv_dynpro_number
              lv_field_name
     CHANGING lv_file_name.

  p_file = lv_file_name.
ENDFORM.                    " fm_val_req_fname

*&---------------------------------------------------------------------*
*&      Form  fm4_filename
*&---------------------------------------------------------------------*
FORM fm_f4_filename
   USING    p_e_program_name
            p_e_dynpro_number
            p_e_field_name
   CHANGING p_i_file_name.

  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  = p_e_program_name
      dynpro_number = p_e_dynpro_number
      field_name    = p_e_field_name
    IMPORTING
      file_name     = p_i_file_name.
ENDFORM.                    " fm4_filename


=========================================================
JSON File Example "C:\json_file.txt"

{"field1":"John", "field2":"Doe", "field3":"Jakarta"},
{"field1":"Anna", "field2":"Smith", "field3":"Bogor"},
{"field1":"Peter", "field2":"Jones", "field3":"Bandung"},
{"field1":"Jane", "field2":"Deity", "field3":"Manado"},
{"field1":"Abdul", "field2":"Kadir", "field3":"Padang"}







=========================================================
Run Program





Result





  

Comments