Displaying JSON
Displaying XML data is rather simple. HTML browsers understand XML too.
DATA itab TYPE TABLE OF i WITH EMPTY KEY.
itab = VALUE #( ( 1 ) ( 2 ) ( 3 ) ).
CALL TRANSFORMATION id SOURCE itab = itab
RESULT XML DATA(xml).
cl_abap_browser=>show_xml( xml_xstring = xml ).
Gives:
But what about JSON?
DATA itab TYPE TABLE OF i WITH EMPTY KEY.
itab = VALUE #( ( 1 ) ( 2 ) ( 3 ) ).
DATA(json_writer) = cl_sxml_string_writer=>create(
type = if_sxml=>co_xt_json ).
CALL TRANSFORMATION id SOURCE itab = itab
RESULT XML json_writer.
DATA(json) = json_writer->get_output( ).
cl_abap_browser=>show_xml( xml_xstring = json ).
Doesn’t work …
cl_abap_browser=>show_html( html_string =
cl_abap_codepage=>convert_from( json ) ).
Works, but JSON is displayed simply as an unformatted text string.
To get a formatted output, for test cases you can use CL_DEMO_OUTPUT.
cl_demo_output=>display_json( json ).
Gives:
The big but: CL_DEMO_OUTPUT is not intended for productive usage.
Now look what I’ve “found”: JSON, Transformation to HTML
You can use the XSL transformation SJSON2HTML in order to transform JSON to formatted HTML and use this with a browser.
CALL TRANSFORMATION sjson2html SOURCE XML json
RESULT XML DATA(html).
cl_abap_browser=>show_html( html_string =
cl_abap_codepage=>convert_from( html ) ).
Gives:
Even interactive. Maybe not perfect but at least something.
SJSON2HTML is available in release 7.02 already.
Hi Horst,
Thanks for the sharing this trick.
it’s a big surprise that SJSON2HTML is available in 7.02. I had to check it to really believe it
Best regards,
Peter
Horst,
I have a doubt… What is the correct way to use the write_json method of the cl_demo_output class? I tried to make a simplified form of what you did above, but without success…
Best regards,
Raphael
First, you have to write
if you want to iterate 4 times.
Second, write_json checks for valid JSON in a string or xstring and does nothing if the input is not JSON. Clearly i is not valid JSON. Neither by data type nor by format.You can output the number with write or write_data:
But you must feed JSON to write_json.
Best
Horst
Thanks for explanation Horst. 🙂
i think the best json serializer and deserializer is
http://wiki.scn.sap.com/wiki/display/Snippets/One+more+ABAP+to+JSON+Serializer+and+Deserializer
that class is really amazing 🙂