Skip to content

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

JSON, asJSON (General)

This example demonstrates asJSON for a text string.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA(text) = `Hello JSON, I'm ABAP!`.
    DATA(out) = cl_demo_output=>new(
      )->write_data( text ).
    DATA(writer) = cl_sxml_string_writer=>create(
      type = if_sxml=>co_xt_json ).
    CALL TRANSFORMATION id SOURCE text = text
                           RESULT XML writer.
    DATA(json) = writer->get_output( ).
    out->write_json( json ).

    out->line( ).

    text = `{"TEXT":"Hello ABAP, I'm JSON!"}`.
    out->write_json( text ).
    CALL TRANSFORMATION id SOURCE XML text
                           RESULT text = text.
    out->write_data( text
      )->display( ).

Description

The identity transformation ID (for which a JSON writer is specified as the XML target) serializes a text string to JSON. The output shows the asJSON format of the text string.

The identity transformation ID then deserializes JSON data in asJSON format to a text string.