Skip to content

ABAP Keyword Documentation →  ABAP − Reference →  Data Interfaces and Communication Interfaces →  ABAP and JSON →  Transformations for JSON →  asJSON - Canonical JSON Representation →  asJSON - Mapping of ABAP Data Types 

asJSON - Mapping of Enumerated Types

The asJSON representation of enumerated types corresponds to their asXML representation. That is, in asJSON the content of an enumerated object is represented by the name (maximum 30 characters) of the enumerated value in uppercase letters, and this is represented as a character-like elementary data object.

Other versions: 7.31 | 7.40 | 7.54


Example

Serialization of an internal table with enumerated values by JSON.

TYPES: 
  BEGIN OF ENUM color STRUCTURE col, 
    red, blue, green, 
  END OF ENUM color STRUCTURE col. 

DATA colors TYPE SORTED TABLE OF color 
            WITH UNIQUE KEY table_line. 

DO. 
  ASSIGN COMPONENT sy-index OF STRUCTURE col TO FIELD-SYMBOL(<fs>). 
  IF sy-subrc <> 0. 
    EXIT. 
  ENDIF. 
  colors = VALUE #( BASE colors ( <fs> ) ). 
ENDDO. 

DATA(writer) = cl_sxml_string_writer=>create( 
     type = if_sxml=>co_xt_json ). 
CALL TRANSFORMATION id SOURCE colors = colors 
                      RESULT XML writer. 

cl_demo_output=>display_json( writer->get_output( ) ).

The output is:

{
"COLORS":
[
  "RED",
  "BLUE",
  "GREEN"
]
}