Find your content:

Search form

You are here

Measuring Size Of output JSON of REST API?

 
Share

I need to get my size of the JSON going as response.I am using recent REST API's of salesforce using apex annotations like @HTTPGET .Can someone please point me out how will i see the size of output JSON using REST EXPLORER of workbench or someother tools if needed .


Attribution to: Mohith Shrivastava

Possible Suggestion/Solution #1

   @HttpGet
    global static List<Account> getAccounts() {
       List<Account> accounts = [select id, name from Account];
       System.debug(JSON.serialize(accounts).length());
        return accounts;
    }

The code above just does the JSON serialisation that SF is going to do automatically, but does it before hand and logs it out to the debug log. It's a bit wasteful and really only for informal checking

Realistically, I would use Curl from the command line as documented here:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_rest_code_sample_basic.htm


Attribution to: Steven Herod

Possible Suggestion/Solution #2

You can look at the Content Length HTTP header to get the length of the response body in bytes. In Workbench REST Explorer, click on Show Raw Response to see the headers on the right hand side of the output.


Attribution to: ryanbrainard

Possible Suggestion/Solution #3

Can you try this

convert the responsebody to a string ( RESPONSE Body (corrected ) is a blob use .tostring())

string name = 'hello this is a test message to find bytes of the string ';
blob encode = EncodingUtil.base64Decode(name);
system.debug('%%%%' + encode.size());

DEBUG LOG :

Execute Anonymous: string name = 'hello this is a test message to find bytes of the string';
Execute Anonymous: blob encode = EncodingUtil.base64Decode(name);
Execute Anonymous: system.debug('%%%%' + encode.size());

10:51:47.092 (92605000)|VARIABLE_ASSIGNMENT|[1]|name|"hello this is a test (36 more) ..."
10:51:47.092 (92619000)|STATEMENT_EXECUTE|[2]
10:51:47.093 (93020000)|SYSTEM_METHOD_ENTRY|[2]|system.EncodingUtil.base64Decode(String)
10:51:47.093 (93165000)|SYSTEM_METHOD_EXIT|[2]|system.EncodingUtil.base64Decode(String)
10:51:47.093 (93469000)|VARIABLE_ASSIGNMENT|[2]|encode|BLOB(33 bytes)

10:51:47.093 (93676000)|SYSTEM_METHOD_ENTRY|[3]|System.debug(ANY)
10:51:47.093 (93693000)|USER_DEBUG|[3]|DEBUG|%%%%33
10:51:47.093 (93702000)|SYSTEM_METHOD_EXIT|[3]|System.debug(ANY)

Attribution to: Rao
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/1890

My Block Status

My Block Content