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


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="">
  <tt:root name="CARRIERS"/>
      <tt:loop ref=".CARRIERS">
          <str name="Carrier-Id">
            <tt:value ref="$ref.carrid"/>
          <str name="Carrier">
            <tt:value ref="$ref.carrname"/>
          <str name="URL">
            <tt:value ref="$ref.url"/>

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.