Skip to Content
Author's profile photo Horst Keller

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.

 

 

 

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Peter Inotai
      Peter Inotai

      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

      Author's profile photo Raphael Pacheco
      Raphael Pacheco

      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…

      TYPES: ref TYPE REF TO if_demo_output.
      
      DATA(test) = REDUCE ref( INIT out = cl_demo_output=>new( )
                                                   FOR i = 1 UNTIL i > 4
                                                   NEXT out = out->write_json( i ) ).
      
      
      test->display( ).

      Best regards,

      Raphael

      Author's profile photo Horst Keller
      Horst Keller
      Blog Post Author

       

      First, you have to write

      FOR i = 1 UNTIL i > 4

       

      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:

      NEXT out = out->write( i )

       

      But you must feed JSON to write_json.

      Best

      Horst

      Author's profile photo Raphael Pacheco
      Raphael Pacheco

      Thanks for explanation Horst. 🙂

      Author's profile photo Bilen Cekic
      Bilen Cekic

      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 🙂