Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *&---------------------------------------------------------------------*
- *& Report ZZ_ENRICH_JSON
- *&---------------------------------------------------------------------*
- *& Modifying JSON data with ABAP
- *&
- *& Scenario:
- *& - Receive some JSON data as XSTRING
- *& - Read them into an XML DOM representation
- *& - Manipulate that XML object (e.g. add elements, change values,...)
- *& - Convert back to XSTRING
- *&
- *&---------------------------------------------------------------------*
- report zz_enrich_json_with_parser.
- class lcl_test definition for testing risk level harmless duration short.
- private section.
- data:
- go_xml type ref to if_ixml,
- go_doc type ref to if_ixml_document.
- methods:
- test for testing,
- string importing iv_str type string returning value(eo_element) type ref to if_ixml_element,
- prepare returning value(ev_json) type xstring.
- endclass.
- class lcl_test implementation.
- method test.
- * An xstring containing some JSON data
- data(lv_json_in) = prepare( ).
- * ----------------------------------------------------------
- * Parse JSON into JSON-XML document
- * ----------------------------------------------------------
- go_xml = cl_ixml=>create( ).
- go_doc = go_xml->create_document( ).
- call transformation id
- source xml lv_json_in
- result xml go_doc.
- data(lo_main) = go_doc->get_root_element( ).
- * ----------------------------------------------------------
- * Example 1: Write some data by manually editing the XML doc
- * ----------------------------------------------------------
- data(lo_new1) = go_doc->create_element( `array` ).
- lo_new1->set_attribute( name = `name` value = `TEST1` ).
- lo_new1->append_child( string(`some`) ).
- lo_new1->append_child( string(`more`) ).
- lo_new1->append_child( string(`JSON data`) ).
- * Add to main doc
- lo_main->append_child( lo_new1 ).
- * ----------------------------------------------------------
- * Example 2: Add some ABAP data
- * There seems to be no efficient way to obtain an XML object
- * directly, containing the data in JSON-XML format
- * ----------------------------------------------------------
- data(lo_more) = cl_sxml_string_writer=>create( if_sxml=>co_xt_json ).
- call transformation id
- source test2 = sy
- result xml lo_more.
- data(lo_more_x) = go_xml->create_document( ).
- data(lv_more) = lo_more->get_output( ).
- call transformation id
- source xml lv_more
- result xml lo_more_x.
- * Add to main doc
- lo_main->append_child(
- lo_more_x->get_root_element( )->get_first_child( )
- ).
- * ----------------------------------------------------------
- * Render result into JSON xstring
- * ----------------------------------------------------------
- data(lo_json) = cl_sxml_string_writer=>create( if_sxml=>co_xt_json ).
- call transformation id
- source xml go_doc
- result xml lo_json.
- data(lv_json_out) = lo_json->get_output( ).
- endmethod.
- method string.
- eo_element = go_doc->create_element( `str` ).
- eo_element->set_value( iv_str ).
- endmethod.
- method prepare.
- ev_json = cl_abap_codepage=>convert_to(
- `{` &&
- ` "TEST":{"some":"more","JSON":"data","number":42,"array":[1,2,3]},` &&
- ` "TESTX":{"important":true}` &&
- `}`
- ).
- endmethod.
- endclass.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement