Skip to Content

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 scarrcarrid,
            carrname TYPE scarrcarrname,
            url      TYPE scarrurl,
          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.

To report this post you need to login first.

10 Comments

You must be Logged on to comment or reply to a post.

  1. Steffen Froehlich

    I couldn’t find CL_DEMO_TEXT=>SHOW_STRING method on a 7.31SP02 CRM system. So I had to implement DISPLAY_AS_TEXT method in a different way, which I want to share here:

        DATA: lv_text TYPE string,
               lt_text TYPE cl_demo_text=>t_text,
               lo_text TYPE REF TO cl_demo_text.

         lv_text = cl_abap_codepage=>convert_from( json ).

         CALL FUNCTION ‘SCMS_STRING_TO_FTEXT’
           EXPORTING
             text            = lv_text
           TABLES
             ftext_tab       = lt_text.

         lo_text = cl_demo_text=>get_handle( ).
         lo_text->add_table( lt_text ).
         lo_text->display(  ).

    Steffen

    (0) 
  2. Martin Ceronio

    Hello Horst, this is a bit off-topic, but anyway: Will Transformations always have to be program objects, or will be ever be able to compile a transformation from a string in an ABAP program? (Or is this perhaps possible now already?)

    (0) 
    1. Horst Keller Post author

      Hi Martin,

      Good Idea, but not yet possible in 7.40 and I don’t know about any plans for that.

      Best Horst

      PS: In the 8.0x-Release-Line (see ABAP News for Release 7.40 – What is ABAP 7.40?) we have already some dynamic transformation features, e.g. a  dynamic mapping in ST. We also have a thing called AMS (ABAP Mapping Script for mappings between ABAP data) that works exactly like you described it (script in a string). Let’s see if some of those will come down to 7.40 somewhere, sometime …

      (0) 
  3. Raghavendra Prabhu Mithal

    Dear Horst,

    Do I need to create different STs for different types of internal tables, My requirement is to convert any internal table to a JSON string. is there a way out, the code should be generic.

    Thanks,

    Raghavendra

    (0) 
    1. Horst Keller Post author

      Hi Raghavendra,

      If you want to convert any internal table to a JSON string, you can use the canonical

      asJSON-Format that defines a predefined mapping of any ABAP data type to JSON.

      You can either use

      • Inside a selfwritten ST, you can use tt:copy to create asXML and from there you can come to JSON-XML for asJSON.

      Best

      Horst

      (0) 
      1. Vijay Gupta

        Hello Keller,

        I need to call an Jason API from SAP ABAP.

        I was trying the code you have provided, but am getting following error  –

        ‘ Transformation “SCARR_2_JSON” has no active version’


        Can you please provide me your e-mail id on which i can share my code with you ? it will be big help.


        Thanks

        Vijay

        (0) 
        1. Horst Keller Post author

          Hi,

          you have to copy the above example transformation into an own Z_…-transformation in SE80 or STRANS and then call this transformation instead of the dummy name SCARR_2_JSON (which does not exist).

          You might also try transformation DEMO_ST_JSON_TABLE that is available in recent releases.

          Horst

          PS: I replaced the transformation name SCARR_2_JSON with the existing transformation DEMO_ST_JSON_TALBE above.

          (0) 

Leave a Reply