Skip to content

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

JSON, asJSON for Elementary ABAP Types

This example demonstrates asJSON for all elementary ABAP types.

Other versions: 7.31 | 7.40 | 7.54

Source Code

    DATA:
      i          TYPE i            VALUE -123,
      int8       TYPE int8         VALUE -123,
      p          TYPE p DECIMALS 2 VALUE `-1.23`,
      decfloat16 TYPE decfloat16   VALUE `123E+1`,
      decfloat34 TYPE decfloat34   VALUE `-3.140000E+02`,
      f          TYPE f            VALUE `-3.140000E+02`,
      c          TYPE c LENGTH 3   VALUE ` Hi`,
      string     TYPE string       VALUE ` Hello `,
      n          TYPE n LENGTH 6   VALUE `001234`,
      x          TYPE x LENGTH 3   VALUE `ABCDEF`,
      xstring    TYPE xstring      VALUE `456789AB`,
      d          TYPE d            VALUE `20020204`,
      t          TYPE t            VALUE `201501`,
      utclong    TYPE utclong      VALUE `2002-02-04T20:15:01,1234567`.

    "Transformation to JSON
    DATA(out) = cl_demo_output=>new(
      )->begin_section( 'asJSON'  ).
    DATA(writer) = cl_sxml_string_writer=>create(
      type = if_sxml=>co_xt_json ).
    DATA stab TYPE abap_trans_srcbind_tab.
    stab = VALUE #(
      ( name = 'I'          value = REF #( i ) )
      ( name = 'INT8'       value = REF #( int8 ) )
      ( name = 'P'          value = REF #( p ) )
      ( name = 'DECFLOAT16' value = REF #( decfloat16 ) )
      ( name = 'DECFLOAT34' value = REF #( decfloat34 ) )
      ( name = 'F'          value = REF #( f ) )
      ( name = 'C'          value = REF #( c ) )
      ( name = 'STRING'     value = REF #( string ) )
      ( name = 'N'          value = REF #( n ) )
      ( name = 'X'          value = REF #( x ) )
      ( name = 'XSTRING'    value = REF #( xstring ) )
      ( name = 'D'          value = REF #( d ) )
      ( name = 'T'          value = REF #( t ) )
      ( name = 'UTCLONG'    value = REF #( utclong ) ) ) .
    CALL TRANSFORMATION id SOURCE (stab)
                           RESULT  XML writer.
    DATA(json) = writer->get_output( ).
    out->write_json( json ).

    "JSON-XML
    out->next_section( 'asJSON-XML' ).
    DATA(reader) = cl_sxml_string_reader=>create( json ).
    DATA(xml_writer) = cl_sxml_string_writer=>create( ).
    reader->next_node( ).
    reader->skip_node( xml_writer ).
    DATA(xml) = xml_writer->get_output( ).
    out->write_xml( xml ).

    "asXML
    out->next_section( 'asXML' ).
    CALL TRANSFORMATION id SOURCE (stab)
                           RESULT  XML xml.
    out->write_xml( xml )->display( ).

Description

The identity transformation ID (for which a JSON writer is specified as the XML target) is called to create and display the asJSON format of all elementary ABAP types.

As a comparison, the JSON-XML representation of the JSON data and the asXML representation of the ABAP data is also shown.