cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

toJSON for RAW data

OhMySAP33
Explorer
0 Likes
1,596

Hello,

Here is the scenario: I execute a JCo call on a function module and call the toJSON() on the output Table or Structure. All the data prints fine except for fields which are of type "RAW".

Here is an example where I am looking at the output of SWNC_GET_WORKLOAD_STATRECS and the field DISPCNT shows up as:
"DISPCNT":"AAE="

If you check the metadata, DISPCNT is of type RAW as shown in the screenshot below. The same happens for few other fields (ex: "WPID":"AAA=", "TASKTYPE":"Cw==", "LUW_INFO":"Fw==") and all of them are of type RAW. I am using JCo version 3.1.7.

Any pointers are very much appreciated!

Accepted Solutions (1)

Accepted Solutions (1)

HAL9000
Product and Topic Expert
Product and Topic Expert

The RAW data bytes are serialized as Base64 string.

OhMySAP33
Explorer
0 Likes

Thanks stefan.gass

Any pointers on how to go about decoding it? I have tried java.util.Base64 decoder as well Apache commons without any luck.

Using Apache Commons: org.apache.commons.codec.binary.Base64;

String encodedString = "sGwAAA==";
Base64 base64 = new Base64();
String decodedString = new String(base64.decode(encodedString.getBytes()));
System.out.println(decodedString);

The above prints output as: �l

Using the java.util.Base64 library

 String encodedString = "sGwAAA==";
byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
String decodedString = new String(decodedBytes);
System.out.println(decodedString);

Prints the output as �l

HAL9000
Product and Topic Expert
Product and Topic Expert
0 Likes

So the decoding worked and you got the raw byte[] again.
What else do you expect when printing raw bytes to the stdout stream?

OhMySAP33
Explorer
0 Likes

stefan.gass - The problem is that the output (after decoding) does not match the expected String. Here is another example:
Here is a snapshot of the raw JSON output received after calling toJSON() on the MAINRECS table received from SWNC_GET_WORKLOAD_SNAPSHOT

{“MAINREC”:{“RECNO”:2,“DISPCNT”:“AAE=“,”ZTTADATE”:“2023-06-02”,“STARTDATE”:“2023-06-02”,“STARTTIME”:“14:06:41”,“STARTTIMESTAMP”:20230602180641,“ENDDATE”:“2023-06-02",“ENDTIME”:“14:06:41",“ENDTIMESTAMP”:20230602180641,“TARGETTIMEZONE”:“EST”,“RESPTI”:3213,“CPUTI”:0,“QUEUETI”:50,“WPID”:“AAA=“,”TASKTYPE”:“Cw==“,”TABLOAD”:“AA==“,”ACCOUNT”:“SAPSYS”,“MANDT”:“000”,“ROLLKEY”:“KT0AAA==“,

As you can see, ROLLKEY shows up as: KT0AAA==

If you look in SAPGUI for this data, ROLLKEY is displayed as 293D0000. Below is a screenshot.

Now when I decode KT0AAA== using either java.util.Base64 or org.apache.commons.codec.binary.Base64 (same java code snippet pasted in earlier comment) in both cases the output is )=

Wondering how do I get what SAP GUI shows: 293D0000

Thanks again!

HAL9000
Product and Topic Expert
Product and Topic Expert

Base64 string "KT0AAA==" represents the 4 bytes: 0x29 0x3D 0x00 0x00
So what is done by this ABAP transaction for the SAP GUI output, is to convert the RAW bytes into a hex string.
If you would like to achieve the same in Java, decode the Base64 string into a byte[], and afterwards convert the byte[] into a hex string.

-- Or simply call myMainrec.getString("ROLLKEY") on the respective JCoStructure object. The JCoRecord.getString() API will also do an on-the-fly conversion from RAW/BYTE type fields to hex strings.

OhMySAP33
Explorer
0 Likes

I get it now and can decode it.

Thanks a lot! stefan.gass

HAL9000
Product and Topic Expert
Product and Topic Expert
0 Likes

No problem.
If your questions are answered, it would be nice, if you upvote this answer and mark it as the correct answer.
Thanks.

Answers (0)