Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
stefan_schnell
Active Contributor
I read here about the possibility to export any result from a function module (FM) in JSON format - a very good idea , many thanks to cesar.martin.

I use the FM RFC_READ_TABLE very often and so I implemented a small extension to export the result also in JSON format.

Here a short two step instruction:

  1. Add in your copy of RFC_READ_TABLE a new export parameter, e.g. DATA_JSON from the type table of lines.

  2. Add at the bottom of your copy of RFC_READ_TABLE, in the line 199 between the last EndSelect and EndIf, the following code.
      "-Export data in JSON format------------------------------------------
    "-
    "- Check the created JSON file with http://www.jsonlint.com validator
    "-
    "---------------------------------------------------------------------

    "-Variables---------------------------------------------------------
    Data strJSON Type SDBLIN1024.
    Data cntData Type i Value 0.
    Data cntLine Type i Value 0.

    Describe Table DATA Lines cntData.

    strJSON = `{`.
    Append strJSON To DATA_JSON.
    Concatenate ` "` QUERY_TABLE '" : [' Into strJSON.
    Append strJSON To DATA_JSON.
    Loop At DATA.
    cntLine = cntLine + 1.

    strJSON = ` {`.
    Append strJSON To DATA_JSON.
    Loop At FIELDS.

    Move DATA-WA+FIELDS-OFFSET(FIELDS-LENGTH) To strJSON.
    Condense strJSON.
    Case FIELDS-TYPE.
    When 'I' Or 'b' Or 's' Or 'P' Or 'F'.
    Len = StrLen( strJSON ) - 1.
    Move strJSON+Len(1) To LastChar.
    If LastChar = '-'.
    Move strJSON+0(Len) To strJSON.
    Concatenate '-' strJSON Into strJSON.
    EndIf.
    Concatenate ` "` FIELDS-FIELDNAME `" : ` strJSON
    Into strJSON.
    When Others.
    Concatenate ` "` FIELDS-FIELDNAME `" : "` strJSON '"'
    Into strJSON.
    EndCase.
    If sy-tabix < sy-tfill.
    Concatenate strJSON ',' Into strJSON.
    EndIf.
    Append strJSON To DATA_JSON.

    EndLoop.
    strJSON = ` }`.
    If cntLine < cntData.
    Concatenate strJSON ',' Into strJSON.
    EndIf.
    Append strJSON To DATA_JSON.

    EndLoop.

    strJSON = ` ]`.
    Append strJSON To DATA_JSON.
    strJSON = `}`.
    Append strJSON To DATA_JSON.

     


With this way you get the result also in JSON format.





Now you can check the validitiy of the JSON output via JSONLint



And now you can easily create JSON images of any table via your copy of RFC_READ_TABLE.

Here an example via COM Connector (CCo) with VBScript:

 
'-Begin-----------------------------------------------------------------

'-Directives----------------------------------------------------------
Option Explicit

'-Constants-----------------------------------------------------------
Const RFC_OK = 0
Const ForWriting = 2

'-Variables-----------------------------------------------------------
Dim SAP, hRFC, rc, hFuncDesc, hFunc, hTable, RowCount, i, hRow
Dim charBuffer, charFile, FSO, F, tableName

'-Main----------------------------------------------------------------
tableName = InputBox("Name of the table", "Request", "SFLIGHT")
Set SAP = CreateObject("COMNWRFC")
If IsObject(SAP) Then
hRFC = SAP.RfcOpenConnection("ASHOST=ABAP, SYSNR=00, " & _
"CLIENT=000, USER=BCUSER")
If hRFC Then
hFuncDesc = SAP.RfcGetFunctionDesc(hRFC, "Z_RFC_READ_TABLE_JSON")
If hFuncDesc Then
hFunc = SAP.RfcCreateFunction(hFuncDesc)
If hFunc Then

rc = SAP.RfcSetChars(hFunc, "QUERY_TABLE", tableName)
rc = SAP.RfcSetChars(hFunc, "DELIMITER", "~")

If SAP.RfcInvoke(hRFC, hFunc) = RFC_OK Then

If SAP.RfcGetTable(hFunc, "DATA_JSON", hTable) = RFC_OK Then
rc = SAP.RfcGetRowCount(hTable, RowCount)
rc = SAP.RfcMoveToFirstRow(hTable)
For i = 1 To RowCount
hRow = SAP.RfcGetCurrentRow(hTable)
rc = SAP.RfcGetChars(hRow, "LINE", charBuffer, 1024)
charFile = charFile & RTrim(charBuffer) & vbCrLf
If i < RowCount Then
rc = SAP.RfcMoveToNextRow(hTable)
End If
Next

'-Write JSON file---------------------------------------
Set FSO = CreateObject("Scripting.FileSystemObject")
If IsObject(FSO) Then
Set F = FSO.OpenTextFile(tableName & ".json", _
ForWriting, True)
F.Write charFile
F.Close
Set FSO = Nothing
End If

End If
End If

rc = SAP.RfcDestroyFunction(hFunc)
End If
End If

rc = SAP.RfcCloseConnection(hRFC)
End If
Set SAP = Nothing
End If

'-End-------------------------------------------------------------------