ABAP Keyword Documentation →  ABAP − Reference →  Data Interfaces and Communication Interfaces →  ABAP and JSON →  JSON, Examples → 

JSON, Simple Transformation for Internal Tables

The example demonstrates the simple transformation of an internal table intoJSON data.

Source Code

REPORT demo_st_json_table.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.
    "Internal table as source of transformation
    DATA(out) = cl_demo_output=>new(
      )->begin_section( 'Internal Table' ).
    DATA: BEGIN OF carrier_wa,
            carrid   TYPE scarr-carrid,
            carrname TYPE scarr-carrname,
            url      TYPE scarr-url,
          END OF carrier_wa,
          carrier_tab LIKE TABLE OF carrier_wa.
    SELECT *
           FROM scarr
           INTO CORRESPONDING FIELDS OF TABLE carrier_tab.
    out->write_data( carrier_tab ).

    "Simple Transformation to JSON
    out->next_section( 'JSON' ).
    DATA(json_writer) = cl_sxml_string_writer=>create(
                          type = if_sxml=>co_xt_json ).
    CALL TRANSFORMATION demo_st_json_table
                        SOURCE carriers = carrier_tab
                        RESULT XML json_writer.
    DATA(json) = json_writer->get_output( ).
    out->write_json( json ).

    "Simple Transformation to JSON-XML
    out->next_section( 'JSON-XML' ).
    CALL TRANSFORMATION demo_st_json_table
                        SOURCE carriers = carrier_tab
                        RESULT XML DATA(xml).
    out->write_xml( xml ).

    "Assert symmetry of transformation
    DATA result_tab LIKE carrier_tab.
    CALL TRANSFORMATION demo_st_json_table
                        SOURCE XML json
                        RESULT carriers = result_tab.
    ASSERT result_tab = carrier_tab.

    "Assert that results of transformation and JSON-Writer are the same
    DATA(xml_reader) = cl_sxml_string_reader=>create( xml ).
    json_writer = cl_sxml_string_writer=>create(
                    type = if_sxml=>co_xt_json ).
    xml_reader->next_node( ).
    xml_reader->skip_node( json_writer ).
    ASSERT json_writer->get_output( ) = json.

    out->display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Description

An internal table, carrier_tab, is filled with data from the database table SCARR and is transformed two times using the simple transformation DEMO_ST_JSON_TABLE. The ST program is as follows:

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">
  <tt:root name="CARRIERS"/>
  <tt:template>
    <array>
      <tt:loop ref=".CARRIERS">
        <object>
          <str name="Carrier-Id">
            <tt:value ref="$ref.carrid"/>
          </str>
          <str name="Carrier">
            <tt:value ref="$ref.carrname"/>
          </str>
          <str name="URL">
            <tt:value ref="$ref.url"/>
          </str>
        </object>
      </tt:loop>
    </array>
  </tt:template>
</tt:transform>

The transformation is written in a way that means the generated XML data have a valid JSON XML format.

Finally, a demonstration that: