Skip to content

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

JSON, Object Components in JSON-XML

This example demonstrates the two alternatives for object components in JSON-XML.

Other versions: 7.31 | 7.40 | 7.54

Source Code

REPORT demo_json_xml_object_members.

CLASS json_demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS json_demo IMPLEMENTATION.
  METHOD main.
    DATA(out) = cl_demo_output=>new(
      )->begin_section( 'Render' ).
    "JSON-XML without and with members
    DATA(json_xml) =
      `<object>` &&
      `<str name="text">abcd</str>` &&
      `<bool name="flag">true</bool>` &&
      `<member name="number">` &&
      `<num>111</num>` &&
      `</member>` &&
      `<member name="content">` &&
      `<null />` &&
      `</member>` &&
      `</object>`.

    out->begin_section(
        'JSON-XML without and with member elements'
      )->write_xml( json_xml ).
    "Render JSON data
    out->next_section( 'JSON' ).
    DATA(reader) = cl_sxml_string_reader=>create(
      input = cl_abap_conv_codepage=>create_out(
                )->convert( json_xml ) ).
    DATA(json_writer) = cl_sxml_string_writer=>create(
                          type = if_sxml=>co_xt_json ) .
    TRY.
        reader->next_node( ).
        reader->skip_node( json_writer ).
      CATCH cx_sxml_parse_error INTO DATA(error).
        out->write_text( error->get_text( ) ).
    ENDTRY.
    DATA(json) = json_writer->get_output( ).
    out->write_json( json
      )->end_section(
      )->next_section( 'Parse' ).

    "Parse JSON data without setting the member option
    out->begin_section( 'JSON-XML without member elements' ).
    DATA(reader1) = cl_sxml_string_reader=>create( input = json ).
    DATA(xml_writer1) = cl_sxml_string_writer=>create( ).
    TRY.
        reader1->next_node( ).
        reader1->skip_node( xml_writer1 ).
      CATCH cx_sxml_parse_error INTO DATA(error1).
        out->write_text( error1->get_text( ) ).
    ENDTRY.
    out->write_xml( xml_writer1->get_output( ) ).

    "Parse JSON data with setting the member option
    out->next_section( 'JSON-XML with member elements' ).
    DATA(reader2) = cl_sxml_string_reader=>create( input = json ).
    reader2->set_option( if_sxml_reader=>co_opt_sep_member ).
    DATA(xml_writer2) = cl_sxml_string_writer=>create( ).
    TRY.
        reader2->next_node( ).
        reader2->skip_node( xml_writer2 ).
      CATCH cx_sxml_parse_error INTO DATA(error2).
        out->write_text( error2->get_text( ) ).
    ENDTRY.
    out->write_xml( xml_writer2->get_output( )
      )->display( ).
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  json_demo=>main( ).

Description

The string json_xml is filled with XML data in valid JSON-XML and displayed. The element <object> contains elements that embody JSON object components directly and <member> elements that wrap these components.

A JSON reader parses the string json_xml by calling the method SKIP_NODE in a single action and passes the XML data to a JSON writer created with the type IF_SXML=>CO_XT_JSON. The output of this writer is passed to the string json, which then contains valid JSON data. The writer has applied both alternatives to the object components with the same result.

The string json is parsed with two further XML readers. The first parses with standard setting and the setting applies the option IF_SXML_READER=>CO_OPT_SEP_MEMBER. In the JSON-XML result of the first reader, no object component has a <member> element; in the JSON-XML results of the second reader, every object component is displayed with a <member> element.