Skip to Content
Author's profile photo Basar Ozgur Kahraman

Serialize ABAP data into JSON format

After reading John Moy ‘s “JqueryMobile and SAP” blog series, i looked for a JSON converter. In community there are several examples and SAP also serves a class: “CL_TREX_JSON_SERIALIZER”.

In my workouts on CL_TREX_JSON_SERIALIZER, i realized that there are two deficiency in standard implementation. Both of them are fixed easily.

1 – There is a space that causes arithmetic problems, at the end of the converted numeric values.

     To fixed this after line 40 in RECURSE method add


     CONDENSE l_value.

2 – Created JSON isn’t valid. CL_TREX_JSON_SERIALIZER doesn’t put the attribute in double quotes.

     To fixed this, comment line 52 in method RECURSE; and add;


     CONCATENATE ‘”‘ <abapcomp>name ‘”‘ c_colon INTO l_value.

Example use of CL_TREX_JSON_SERIALIZER:

DATA: it_usr01       TYPE STANDARD TABLE OF usr01,

          lo_json_data TYPE REF TO zcl_trex_json_serializer,

          json             TYPE string.

SELECT *

INTO TABLE it_usr01

FROM usr01

CREATE OBJECT lo_json_data

   EXPORTING

     DATA = it_usr01.

* serialize data

   lo_json_data->serialize( ).

* get serialized json data string

   json = lo_json_data->get_data( ).

WRITE json.

New implementation of RECURSE method:

method RECURSE.

   data:

     l_type  type c ,

     l_comps type i ,

     l_lines type i ,

     l_index type i ,

     l_value type string .

   field-symbols:

     <itab> type any table ,

     <comp> type any .

   describe field data type l_type components l_comps .

   if l_type = cl_abap_typedescr=>typekind_table .

*   itab -> array

     append ‘[‘ to me->fragments .

     assign data to <itab> .

     l_lines = lines( <itab> ) .

     loop at <itab> assigning <comp> .

       add 1 to l_index .

       recurse( <comp> ) .

       if l_index < l_lines .

         append c_comma to me->fragments .

       endif .

     endloop .

     append ‘]’ to fragments .

   else .

     if l_comps is initial .

*     field -> scalar

*     todo: format

       l_value = data .

       replace all occurrences of ‘\’ in l_value with ‘\\’ .

       replace all occurrences of in l_value with ‘\’.

       replace all occurrences of ‘”‘ in l_value with ‘\”‘ .

       replace all occurrences of ‘&’ in l_value with ‘\&’ .

       replace all occurrences of cl_abap_char_utilities=>cr_lf in l_value with ‘\r\n’ .

       replace all occurrences of cl_abap_char_utilities=>newline in l_value with ‘\n’ .

       replace all occurrences of cl_abap_char_utilities=>horizontal_tab in l_value with ‘\t’ .

       replace all occurrences of cl_abap_char_utilities=>backspace in l_value with ‘\b’ .

       replace all occurrences of cl_abap_char_utilities=>form_feed in l_value with ‘\f’ .

*     space at the end of numbers

       CONDENSE l_value.

       concatenate ‘”‘ l_value ‘”‘ into l_value .

       append l_value to me->fragments .

     else .

*     structure -> object

       data l_typedescr type ref to cl_abap_structdescr .

       field-symbols <abapcomp> type abap_compdescr .

       append ‘{‘ to me->fragments .

       l_typedescr ?= cl_abap_typedescr=>describe_by_data( data ) .

       loop at l_typedescr->components assigning <abapcomp> .

         l_index = sytabix .

*         concatenate <abapcomp>-name c_colon into l_value .

         CONCATENATE ‘”‘ <abapcomp>name ‘”‘ c_colon INTO l_value .

         translate l_value to lower case .

         append l_value to me->fragments .

         assign component <abapcomp>name of structure data to <comp> .

         recurse( <comp> ) .

         if l_index < l_comps .

           append c_comma to me->fragments .

         endif .

       endloop .

       append ‘}’ to me->fragments .

     endif .

   endif .

endmethod.

Assigned Tags

      10 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Graham Robinson
      Graham Robinson

      Good one. Thanks. 😎

      Author's profile photo Chris Paine
      Chris Paine

      Seems like a similar bug to one that we fixed in ZJSON about a year ago πŸ˜‰

      Which is one reason I like the open-sourced solutions, bugs get fixed so much quicker.

      Author's profile photo Alessandro Spadoni
      Alessandro Spadoni

      Check the ABAP Json document class project on Code Exchange by Uwe Fetzer https://cw.sdn.sap.com/cw/groups/zjson

      I suggest to use zjson instead of cl_trex_json_serializer

      Author's profile photo Veronika Hermenau
      Veronika Hermenau

      It was great - looking for comments to the class and finding directly the solution for our Problem πŸ™‚ ➕ ➕

      Author's profile photo Basar Ozgur Kahraman
      Basar Ozgur Kahraman
      Blog Post Author

      Hi Veronika,

      it is very nice to hear that you found solution directly, Power of SCN πŸ™‚

      Author's profile photo Former Member
      Former Member

      Hello Basar  sir,

      how can we deseralize json format webservice in abap

      Author's profile photo Basar Ozgur Kahraman
      Basar Ozgur Kahraman
      Blog Post Author

      Hello Muhammad,

      you can check ZJSON- JSON and ABAP - Code Gallery - SCN Wiki

      Author's profile photo Michael Howles
      Michael Howles

      Thanks - Exactly my problem (missing quotes on attributes).  You rock.

      EDIT: Also, I think this line should change from:

      replace all occurrences of '&' in l_value with '\&'


      to:


      replace all occurrences of '&' in l_value with '&amp;'


      At least this was my case, the JSON wouldn't parse otherwise.

      Author's profile photo Kunal Jauhari
      Kunal Jauhari

      helped me a lot πŸ™‚

      Author's profile photo Felipe Augusto Yansen
      Felipe Augusto Yansen

      Hi everybody.

      how about the classΒ cl_trex_json_deserializer, does anybody knows how to fix its bugs?

       

      tks

      Felipe