Skip to content

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

JSON, Simple Transformation for Name Attributes

The example demonstrates a simple transformation used to create name attributes in JSON 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,
            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_attributes
                        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_attributes
                        SOURCE carriers = carrier_tab
                        RESULT XML DATA(xml).
    out->write_xml( xml ).


    out->display( ).

Description

This example works in a similar way to the executable example for an internal table, but the values of the attributes name of the elements object in JSON-XML are also filled from the internal table in the simple transformation DEMO_ST_JSON_TABLE_ATTRIBUTES. 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>
            <tt:attribute name="name">
              <tt:value ref="$ref.carrname"/>
            </tt:attribute>
            <tt:value ref="$ref.url"/>
          </str>
        </object>
      </tt:loop>
    </array>
  </tt:template>
</tt:transform>