
"-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.
'-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-------------------------------------------------------------------
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
4 | |
3 | |
3 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 |