ABAP Keyword Documentation → ABAP − Reference → Data Interfaces and Communication Interfaces → ABAP and JSON
Parsing and Rendering JSON Data
The processing of JSON data in ABAP is integrated into sXML Library. Here, JSON is supported as a separate format, with JSON-XML used as an intermediate step.
Other versions:
7.31 | 7.40 | 7.54
Parsing JSON Data
The method CREATE can be used to pass both XML data and JSON data to each XML reader in sXML Library (except for XOP readers) that is based on the appropriate classes and interfaces. The reader recognizes the JSON format and becomes a JSON reader. A JSON reader handles JSON data as if it were passed XML data that contains the JSON-XML representation of the JSON data.
When parsed, invalid JSON data raises an exception of the class CX_SXML_PARSE_ERROR, which must be caught in each parser step.
Notes
- A JSON reader can, like every XML reader, be used as the source of the statement
CALL TRANSFORMATION
, which permits direct serialization of JSON data to ABAP data objects.
- When JSON data is parsed, escape characters \ in front of special characters in character-like values are removed automatically.
- By default, the JSON-XML representation is created internally, where the object components of the JSON data are not nested in an element <member>. To create the longer variant with <member> elements, the method SET_OPTION of the interface IF_SXML_READER can be used to set the option IF_SXML_READER=>CO_OPT_SEP_MEMBER.
Executable Example
Rendering of JSON Data
The method CREATE can be used to create each XML writer in sXML Library (except for XOP writers) that is based on the appropriate classes and interfaces as a JSON writer. Here, the associated constant of the interface IF_SXML must be passed to the input parameter TYPE.
cl_sxml_..._writer=>create( type = if_sxml=>co_xt_json ).
A JSON writer of this type can, like any sXML writer, be provided with XML data using token-based and object-oriented rendering methods. This XML data must be a valid JSON-XML representation of JSON data. The object components of JSON data can be written to a writer in either JSON-XML notation, with or without <member> element.
The writer transforms the JSON-XML representation entered to JSON format and provides the result as specified by the writer type.
Invalid JSON data is not created and raises an exception of the class CX_SXML_STATE_ERROR, which must be caught in each writer step.
Notes
- When JSON data is rendered, special characters in character-like values are prefixed automatically with the escape character \.
- A JSON writer can, like every XML writer, be used as the target of the statement
CALL TRANSFORMATION
, which permits direct serialization of ABAP data objects to JSON data.