Skip to main content

ABAP to JSON / SAP Internal Table to JSON

ABAP TO JSON Parser:
There is a standard class : CL_TREX_JSON_SERIALIZER available to convert Internal table to JSON format.
Usage:
DATA lr_json TYPE REF TO cl_trex_json_serializer.


  
CREATE OBJECT lr_json
    
EXPORTING
      
data lt_accounts.

  lr_json
->serialize( ).

  
DATA lv_jsonstr TYPE string.


  lv_jsonstr 
lr_json->get_data( ).



The standard class as is might not be give you an acceptable JSON format, make the following enhancements to fix this:
  1. Using Enhancement Framework, add a new Method “ZRECURSE” copying “RECURSE”
 DATA:
    l_type  
TYPE c ,
    l_comps 
TYPE i ,
    l_lines 
TYPE i ,
    l_index 
TYPE i ,
    l_value 
TYPE string .
  
FIELD-SYMBOLS:
     
TYPE ANY TABLE ,
     
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  .
    l_lines 
lines ) .
    
LOOP AT  ASSIGNING  .
      
ADD TO l_index .
      zrecurse
 ) .
      
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' .
      
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  TYPE abap_compdescr .

      
APPEND '{' TO me->fragments .
      l_typedescr ?= cl_abap_typedescr
=>describe_by_datadata ) .
      
LOOP AT l_typedescr->components ASSIGNING  .
        l_index 
sy-tabix .
        
CONCATENATE '"' -name '"'  c_colon INTO l_value .
        
TRANSLATE l_value TO LOWER CASE .
        
APPEND l_value TO me->fragments .
        
ASSIGN COMPONENT -name OF STRUCTURE data TO  .
        zrecurse
 ) .
        
IF l_index < l_comps .
          
APPEND c_comma TO me->fragments .
        
ENDIF .
      
ENDLOOP .
      
APPEND '}' TO me->fragments .
    
ENDIF .
  
ENDIF .
  1. Create a new method “ZSERIALIZE”  copying method “SERIALIZE” and put the below code
METHOD zserialize .
  
FIELD-SYMBOLS  TYPE data .

  
ASSIGN me->data_ref->TO  .
  zrecurse
 ) .
ENDMETHOD.
  1. Now change the usage to the new method “zserialize” instead of “serialize” as shown below
  DATA lr_json TYPE REF TO cl_trex_json_serializer.


  
CREATE OBJECT lr_json
    
EXPORTING
      
data lt_accounts.

  lr_json
->zserialize( ).

  
DATA lv_jsonstr TYPE string.


  lv_jsonstr 
lr_json->get_data( ).

Comments

  1. sir how can we consme json format webservice in abap code.

    ReplyDelete
  2. In source code, all field symbols is disappeared. Please trubleshoot it.

    ReplyDelete

Post a Comment

Popular posts from this blog

SAP Add custom fields to VA05

You don't need to make a copy of VA05 to add custom fields, here are the instructions to do it

To add your field as one of the column on the output list, add it to structure  VBMTV            Add the logic to fill the value in user-exit VV05TFZ3/FORM END_MODIFICATION.
          Update the work area lvbmtv with the value what you want to display
To add to the selection-screen make an entry in the config table shown as below
               SPRO->Sales Distribution->Sales->List->Define selection criteria

              Added SAPMV75A 0 K SELK XXXXX  where XXXXX is your custom field



SAP Content Server reference

1. Create a Content Repository OAC0
2. Create a Documnet Type - OAD5,
        BUS1022      Fixed asset

SPRO->SAP NETWEAVER->APPLICATION SERVER->BASIS SERVICES
  ->KNOWLEDGE PROVIDER


Get all attachments at once


Program OACONTA4, gives list of  Documents  for a Business Object
  FM: ARCHIV_GET_CONNECTIONS
          Oject Type = BUS1022
          documenttype = ZAST

Table: toasp - ZAST Details
TOJTT- Table for BO details

Possible Connection tables

TOA01
TOA02
TOA03
TOAHR

Debug points from Application
  FM-GOS_EXECUTE_SERVICE
    -

    http://help.sap.com/saphelp_nw04/helpdata/en/59/fba637fcf7dc39e10000009b38f8cf/frameset.htm

    Applications that use the technical infrastructure of the SAP Content Server include the SAP Business Workplace, SAP ArchiveLink, the Document Management System (DMS), and the Knowledge Warehouse.


ArchiveLink is a service integrated in the SAP Web Application Server for linking archived documents and the application documents entered in the SAP system.


Archive Link Help

h…