Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
horst_keller
Product and Topic Expert
Product and Topic Expert

Facts

  • Simple Transformations (ST) serialize ABAP to XML and deserialize XML to ABAP.
  • JSON-writers of the sXML-Library consume JSON-XML and digest it to JSON.
  • The statement CALL TRANSFORMATION handles JSON-sources as JSON-XML implicitly.

Conclusion

The result of an ST that produces JSON-XML can be fed into a JSON writer to produce JSON. The same transformation can be used to deserialize JSON directly (a JSON-reader could be switched in between, but is not necessary).

Example ST producing or consuming JSON-XML

<?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>

Example ABAP calling ST

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
  PRIVATE SECTION.
    CLASS-METHODS: display_as_text
                    IMPORTING json TYPE xstring,
                  display_as_html
                    IMPORTING json TYPE xstring.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.

    "Get some data into an internal table
    TYPES: BEGIN OF carrier,
            carrid  TYPE scarr-carrid,
            carrname TYPE scarr-carrname,
            url      TYPE scarr-url,
          END OF carrier.
    DATA carriers TYPE TABLE OF carrier.
    SELECT carrid carrname url
          FROM scarr
          INTO CORRESPONDING FIELDS OF TABLE carriers.

    "Create JSON-writer and call simple transformation
    DATA json_writer TYPE REF TO cl_sxml_string_writer.
    DATA writer TYPE REF TO if_sxml_writer.
    json_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
    writer ?= json_writer.
    writer->set_option( option = if_sxml_writer=>co_opt_linebreaks ).
    writer->set_option( option = if_sxml_writer=>co_opt_indent ).
    CALL TRANSFORMATION demo_st_json_table SOURCE carriers = carriers
                                    RESULT XML json_writer.

    "Get JSON-String from writer
    DATA json TYPE xstring.
    json = json_writer->get_output( ).

    "Two ways of displaying the JSON string
    display_as_text( json ).
    display_as_html( json ).

    "Back to ABAP, verify that transformation is symmetric

    DATA result LIKE carriers.
    CALL TRANSFORMATION demo_st_json_table SOURCE XML json
                                    RESULT carriers = result.
   
ASSERT result = carriers.

  ENDMETHOD.
  METHOD display_as_text.
    "Simple output of JSON in text edit control
    DATA text TYPE string.
    text = cl_abap_codepage=>convert_from( json ).
    cl_demo_text=>show_string( text ).
  ENDMETHOD.
  METHOD display_as_html.
    "Beautified output of JSON in HTML browser
    DATA html TYPE string.
    CALL TRANSFORMATION sjson2html SOURCE XML json
                                  RESULT XML html.
    cl_abap_browser=>show_html( html_string = html ).
  ENDMETHOD.
ENDCLASS.

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

Note

If you replace type = if_sxml=>co_xt_json with type = if_sxml=>co_xt_xml10 when creating the sXML-writer, it becomes an XML-writer and you will get JSON-XML instead of JSON from the writer.

14 Comments