Skip to content

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.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    "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( ).

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.

  • When the transformation is called for the first time, a JSON writer is specified as the result. The JSON data can be retrieved from this result and added to the json string, where the data can be output.
  • When the transformation is called for the second time, an inline declared byte string xml is specified as the result. This is usually used to generate XML data in XML 1.0 format. The output shows that it is the JSON XML generated by the transformation.

Finally, a demonstration that:

  • the simple transformation is symmetrical. The JSON data is passed to the transformation. The result matches the starting table.
  • An additional JSON writer creates the same JSON data in xml from the JSON XML, as when the transformation is called. Specifying a JSON writer as the result of a transformation means that JSON XML does not need to be explicitly handled in the ABAP program.