Advertisement
rplantiko

Manipulate JSON data in a proxy

Apr 24th, 2019
1,207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
ABAP 3.34 KB | None | 0 0
  1. *&---------------------------------------------------------------------*
  2. *& Report ZZ_ENRICH_JSON
  3. *&---------------------------------------------------------------------*
  4. *& Modifying JSON data with ABAP
  5. *&
  6. *& Scenario:
  7. *& - Receive some JSON data as XSTRING
  8. *& - Read them into an XML DOM representation
  9. *& - Manipulate that XML object (e.g. add elements, change values,...)
  10. *& - Convert back to XSTRING
  11. *&
  12. *&---------------------------------------------------------------------*
  13. report zz_enrich_json_with_parser.
  14.  
  15. class lcl_test definition for testing risk level harmless duration short.
  16.   private section.
  17.     data:
  18.       go_xml type ref to if_ixml,
  19.       go_doc type ref to if_ixml_document.
  20.     methods:
  21.       test for testing,
  22.       string importing iv_str type string returning value(eo_element) type ref to if_ixml_element,
  23.       prepare returning value(ev_json) type xstring.
  24.  
  25. endclass.
  26.  
  27. class lcl_test implementation.
  28.   method test.
  29.  
  30. * An xstring containing some JSON data
  31.     data(lv_json_in) = prepare( ).
  32.  
  33. * ----------------------------------------------------------
  34. * Parse JSON into JSON-XML document
  35. * ----------------------------------------------------------
  36.     go_xml = cl_ixml=>create( ).
  37.     go_doc = go_xml->create_document( ).
  38.     call transformation id
  39.       source xml lv_json_in
  40.       result xml go_doc.
  41.  
  42.     data(lo_main) = go_doc->get_root_element( ).
  43.  
  44. * ----------------------------------------------------------
  45. * Example 1: Write some data by manually editing the XML doc
  46. * ----------------------------------------------------------
  47.     data(lo_new1)  = go_doc->create_element( `array` ).
  48.     lo_new1->set_attribute( name = `name` value = `TEST1` ).
  49.     lo_new1->append_child( string(`some`) ).
  50.     lo_new1->append_child( string(`more`) ).
  51.     lo_new1->append_child( string(`JSON data`) ).
  52. * Add to main doc
  53.     lo_main->append_child( lo_new1 ).
  54.  
  55. * ----------------------------------------------------------
  56. * Example 2: Add some ABAP data
  57. * There seems to be no efficient way to obtain an XML object
  58. * directly, containing the data in JSON-XML format
  59. * ----------------------------------------------------------
  60.     data(lo_more) = cl_sxml_string_writer=>create( if_sxml=>co_xt_json ).
  61.     call transformation id
  62.       source test2 = sy
  63.       result xml lo_more.
  64.     data(lo_more_x) = go_xml->create_document( ).
  65.     data(lv_more) = lo_more->get_output( ).
  66.     call transformation id
  67.       source xml lv_more
  68.       result xml lo_more_x.
  69. * Add to main doc
  70.     lo_main->append_child(
  71.       lo_more_x->get_root_element( )->get_first_child( )
  72.     ).
  73.  
  74. * ----------------------------------------------------------
  75. * Render result into JSON xstring
  76. * ----------------------------------------------------------
  77.     data(lo_json) = cl_sxml_string_writer=>create( if_sxml=>co_xt_json ).
  78.     call transformation id
  79.       source xml go_doc
  80.       result xml lo_json.
  81.  
  82.     data(lv_json_out) = lo_json->get_output( ).
  83.  
  84.   endmethod.
  85.   method string.
  86.     eo_element = go_doc->create_element( `str` ).
  87.     eo_element->set_value( iv_str ).
  88.   endmethod.
  89.   method prepare.
  90.     ev_json = cl_abap_codepage=>convert_to(
  91.       `{` &&
  92.       `  "TEST":{"some":"more","JSON":"data","number":42,"array":[1,2,3]},` &&
  93.       `  "TESTX":{"important":true}` &&
  94.       `}`
  95.     ).
  96.   endmethod.
  97.  
  98. endclass.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement