Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to Display a Json-String in SAP-Browser

majcon
Active Participant
0 Kudos

Hi,

http://scn.sap.com/people/horst.keller/blog/2013/01/07/abap-and-json

after executing the above example I got an String with the Json-Interpretation.

Is there any SAP-Standard Functionality to Display the Json as elegant as XML-String:

for example

   cl_abap_browser=>show_xml( exporting xml_string = iv_xml_string ).

Thanks, for your help.

Cheers,

Damir

only for Information sap-system:

SAP_BASIS    702    0012    SAPKB70212    SAP Basis Component

SAP_ABA    702    0012    SAPKA70212    Cross-Application Component

1 ACCEPTED SOLUTION

horst_keller
Product and Topic Expert
Product and Topic Expert

Hi Damir,

thanx for declaring cl_abap_browser as "elegant'" , it is a class that I've programmed once for my own purposes for displaying html in examples of the ABAP Documentation and now it is "SAP-Standard Functionality" . Maybe it's because of the name ....

Well, cl_abap_browser is simply a convenience tool that encapsulates CL_GUI_HTML_VIEWER. Since the browser used by CL_GUI_HTML_VIEWER understands XM automatically, it was simple to offer SHOW_XML-methods.

For JSON it is not that simple. If you have transaction STDEMO (program STRANSDEMO_FLIGHTS) in your system, you can see an individual example, where JSON-data are formatted for display. The trick used there is to call an XSLT-program SJSON2HTML that transforms JSON (aka JSON-XML) to HTML. And no, that is not my program ...

For my own purposes, I followed a more simplistic way. You can introduce linebreaks and indents into any JSON-string as follows:

       

        lv_xjson = ...

        DATA(reader) = cl_sxml_string_reader=>create( lv_xjson ).
        DATA(writer) = CAST if_sxml_writer(
                              cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ) ).
        writer->set_option( option = if_sxml_writer=>co_opt_linebreaks ).
        writer->set_option( option = if_sxml_writer=>co_opt_indent ).
        reader->next_node( ).
        reader->skip_node( writer ).
       
DATAT(lv_json) = cl_abap_codepage=>convert_from(

           CAST cl_sxml_string_writer( writer )->get_output( ) ).

(OK, ok, you might not know what DATA( ) and CAST is about. DATA( ) is a new operator for inline declarations coming with release 7.40 that you must replace with DATA statements in older releases. CAST is a new casting operator coming with release 7.40 that you must replace with ?= and helper variables in older releases.)

After having the linebreaks and indents in JSON, it is not so complicated any more to transform that to html (string templates |\n| can be involved here ).

Currently I have no plans to extent CL_ABAP_BROWSER with a SHOW_JSON-Method, since it should be kept as wrapper for browser functionality. If browsers format JSON one day, I might add it.

Instead, in releases 7.31/7.40 I am "playing around" with a small framework to replace WRITE-statements in the example programs of the ABAP Documentation.

With that, a formatted (but not interactive) output for JSON can be achieved as follows (as of 7.31 SP something):

  cl_demo_output=>display_json( json ).

But  - of course - CL_DEMO_OUTPUT is not released for productive usage ...

12 REPLIES 12

horst_keller
Product and Topic Expert
Product and Topic Expert

Hi Damir,

thanx for declaring cl_abap_browser as "elegant'" , it is a class that I've programmed once for my own purposes for displaying html in examples of the ABAP Documentation and now it is "SAP-Standard Functionality" . Maybe it's because of the name ....

Well, cl_abap_browser is simply a convenience tool that encapsulates CL_GUI_HTML_VIEWER. Since the browser used by CL_GUI_HTML_VIEWER understands XM automatically, it was simple to offer SHOW_XML-methods.

For JSON it is not that simple. If you have transaction STDEMO (program STRANSDEMO_FLIGHTS) in your system, you can see an individual example, where JSON-data are formatted for display. The trick used there is to call an XSLT-program SJSON2HTML that transforms JSON (aka JSON-XML) to HTML. And no, that is not my program ...

For my own purposes, I followed a more simplistic way. You can introduce linebreaks and indents into any JSON-string as follows:

       

        lv_xjson = ...

        DATA(reader) = cl_sxml_string_reader=>create( lv_xjson ).
        DATA(writer) = CAST if_sxml_writer(
                              cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ) ).
        writer->set_option( option = if_sxml_writer=>co_opt_linebreaks ).
        writer->set_option( option = if_sxml_writer=>co_opt_indent ).
        reader->next_node( ).
        reader->skip_node( writer ).
       
DATAT(lv_json) = cl_abap_codepage=>convert_from(

           CAST cl_sxml_string_writer( writer )->get_output( ) ).

(OK, ok, you might not know what DATA( ) and CAST is about. DATA( ) is a new operator for inline declarations coming with release 7.40 that you must replace with DATA statements in older releases. CAST is a new casting operator coming with release 7.40 that you must replace with ?= and helper variables in older releases.)

After having the linebreaks and indents in JSON, it is not so complicated any more to transform that to html (string templates |\n| can be involved here ).

Currently I have no plans to extent CL_ABAP_BROWSER with a SHOW_JSON-Method, since it should be kept as wrapper for browser functionality. If browsers format JSON one day, I might add it.

Instead, in releases 7.31/7.40 I am "playing around" with a small framework to replace WRITE-statements in the example programs of the ABAP Documentation.

With that, a formatted (but not interactive) output for JSON can be achieved as follows (as of 7.31 SP something):

  cl_demo_output=>display_json( json ).

But  - of course - CL_DEMO_OUTPUT is not released for productive usage ...

majcon
Active Participant
0 Kudos

Hi Horst,

thank you for your helpful answer! It direct´s me to the right direction

Best regards,

Damir

0 Kudos

Damir, the debugger uses the XSLT transformation SJSON2HTML to visualize JSON. Try it out: When you pass a JSON data object in its SAP-internal XML representation ( with <object>'s, <array>'s, <str>'s and so on), this transformation creates HTML code which visualizes it. You can use this transformation, and then pass the created HTML code to an HTML control.

Regards,

Rüdiger

For SAP-employees, the author gave his D-number 🙂 d026228

0 Kudos

OK, ok, you might not know what DATA( ) and CAST is about. DATA( ) is a new operator for inline declarations coming with release 7.40 that you must replace with DATA statements in older releases. CAST is a new casting operator coming with release 7.40 that you must replace with ?= and helper variables in older releases

Thanks for disclosing a few 740 release offerings

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

You wouldn't believe your eyes if I'd publish a fully fledged 7.40 ABAP program here ...

rdiger_plantiko2
Active Contributor

For illustration, here is a screenshot of the result of the built-in transformation SJSON2HTML on a JSON object in the debugger. I like it - it is even interactive: You can open and close nodes.

0 Kudos

Hi Rüdiger,

thanks for the explanation an the further information 😉

Cheers,

Damir

0 Kudos

Hi Rüdiger,

I've tried to check how that "Detail display" is processed, but you can't debug a debugger 😉
Could you please let me know how you've achieved that display in the debugger?
Have you enhanced cl_abap_browser class?

Thanks in advance.

Adam

0 Kudos

Hi Adam, if the field that you want to display is an XSTRING holding an UTF8-encoded valid JSON-Objekt, then it should be displayed in the above style in the view "XML Browser". As mentioned, the XSLT transformation SJSON2HTML is used internally to generate the html code that then is displayed in the HTML viewer control.

0 Kudos

Addition to my last comment: This should be standard behaviour. I didn't change CL_ABAP_BROWSER or did anything sophisticated. The detection that this XSTRING is an UTF-8 encoded JSON object goes behind the scenes.

0 Kudos

Hi Rüdiger,
thanks for your explanation.
My fault was trying to display STRING like that.

I've changed to XSTRING and all of a sudden magic is happening 🙂

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

A simple one ...

DATA json_writer TYPE REF TO cl_sxml_string_writer.
DATA writer TYPE REF TO if_sxml_writer.
json_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
writer ?= json_writer.
writer->set_option( option = if_sxml_writer=>co_opt_linebreaks ).
writer->set_option( option = if_sxml_writer=>co_opt_indent ).
CALL TRANSFORMATION id SOURCE ...
                       RESULT XML json_writer.

DATA json TYPE string.
json = cl_abap_codepage=>convert_from( json_writer->get_output( ) ).

cl_demo_text=>show_string( json ).