2013 Apr 09 3:00 PM
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
2013 Apr 10 9:05 AM
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 ...
2013 Apr 10 9:05 AM
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 ...
2013 Apr 10 9:26 AM
Hi Horst,
thank you for your helpful answer! It direct´s me to the right direction
Best regards,
Damir
2013 Apr 10 2:27 PM
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
2013 Apr 11 8:27 AM
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
2013 Apr 11 8:34 AM
You wouldn't believe your eyes if I'd publish a fully fledged 7.40 ABAP program here ...
2013 Apr 11 3:59 AM
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.
2013 Apr 11 7:01 PM
Hi Rüdiger,
thanks for the explanation an the further information 😉
Cheers,
Damir
2021 Jan 15 1:44 PM
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
2021 Jan 15 1:49 PM
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.
2021 Jan 15 1:51 PM
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.
2021 Jan 21 8:25 AM
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 🙂
2013 Apr 13 10:53 AM
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 ).
2024 May 06 10:36 PM
Hello,
This answer helped me to solve a problem.
DATA:
lv_xstring TYPE xstring,
lv_xjson TYPE xstring,
lv_json TYPE string,
lo_reader TYPE REF TO if_sxml_reader,
lo_json_writer TYPE REF TO cl_sxml_string_writer,
lo_writer TYPE REF TO if_sxml_writer.
lv_xstring = " json xstring content
" Create writer for json
lo_json_writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ).
" set linebreak and indentation
lo_writer ?= lo_json_writer.
lo_writer->set_option( option = if_sxml_writer=>co_opt_linebreaks ).
lo_writer->set_option( option = if_sxml_writer=>co_opt_indent ).
" Create reader for xstring content
lo_reader = cl_sxml_string_reader=>create( lv_xstring ).
lo_reader->next_node( ).
lo_reader->skip_node( lo_writer ).
" Get formatted xstring
lv_xjson = lo_json_writer->get_output( ).
" Get formatted string
lv_json = cl_abap_codepage=>convert_from( lx_xjson ).
" Display content
cl_demo_text=>show_string( lv_json ).
My solution is something like above code. Get a json xstring and get a formatted content, but instead of using CALL TRANSFORMATION, I use `SXML_READER` to get content and pass to writer.
Maybe it can help someone else.
Regards,
Danilo