ABAP Keyword Documentation → ABAP − Reference → Data Interfaces and Communication Interfaces → ABAP and JSON → JSON, Examples
JSON, Identity Transformation with JSON Writer as Target
This example demonstrates various XML sources for identity transformations with a JSON writer as a target.
Other versions:
7.31 | 7.40 | 7.54
Source Code
DATA(out) = cl_demo_output=>new(
)->begin_section(
`Identity Transformation for JSON Writer` ).
DATA json_writer TYPE REF TO cl_sxml_string_writer.
out->begin_section(
`Source JSON String` ).
DATA(json) = cl_abap_conv_codepage=>create_out( )->convert(
`{"TEXT":"Hello JSON!"}` ).
json_writer = cl_sxml_string_writer=>create(
type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE XML json
RESULT XML json_writer.
out->write_json( json_writer->get_output( ) ).
out->next_section(
`Source JSON Reader` ).
DATA(json_reader) = cl_sxml_string_reader=>create( json ).
json_writer = cl_sxml_string_writer=>create(
type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE XML json_reader
RESULT XML json_writer.
out->write_json( json_writer->get_output( ) ).
out->next_section(
`Source JSON-XML` ).
DATA(xml_json) = cl_abap_conv_codepage=>create_out( )->convert(
`<object><str name="TEXT">Hello JSON!</str></object>` ).
json_writer = cl_sxml_string_writer=>create(
type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE XML xml_json
RESULT XML json_writer.
out->write_json( json_writer->get_output( )
)->display( ).
Description
The identity transformation ID is filled three times with XML sources. These sources can be passed to the JSON writer specified as an XML target:
- Valid JSON data in a byte string.
- A JSON reader that points to this byte string.
- XML data in a byte string in JSON XML format.