function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
David BermanDavid Berman 

Querying JSON data fails without explanation.

I'm connecting to Mavenlink to pull time cards  and running the below code in the Developer Console.  2/3s down the page my last if-statement block of code causes the code to fail without any explanation.  Commenting out that if-statement block causes a successful execution.

I hope someone can help..I hope it's a silly little thing I missed and I can stop banging my head in the wall....
 
httpRequest reqWorkspaces = new httpRequest();
reqWorkspaces.setMethod('GET');
reqWorkspaces.setEndpoint('https://api.mavenlink.com/api/v1/time_entries.json'+
                          '?created_after=2015-08-01T000000&created_before=2015-08-16T000000&page=1&per_page=20');
reqWorkspaces.setHeader('Authorization', 'Bearer xxxxxxxx');
reqWorkspaces.setTimeout(120000);
httpResponse resWorkspaces = new http().send(reqWorkspaces);

JSONParser parserCount = JSON.createParser(resWorkspaces.getBody());
Integer iCountTimecards = 0;
Integer iNumberOfBatches = 0;

while(parserCount.nextToken() != null)
{
	if((parserCount.getCurrentToken() == JSONToken.FIELD_NAME) && (parserCount.getText() == 'count'))
    {
        parserCount.nextToken();
        iCountTimecards = Integer.valueOf(parserCount.getText());
    }
}

SYSTEM.DEBUG('+++++ iCountTimecards: ' + iCountTimecards);

Map<String, String> mapKeyValue = new Map<String, String>();
Map<String, Map<String, String>> mapJSON = new Map<String, Map<String, String>>();

if(iCountTimecards > 0)
{
	iNumberOfBatches = Math.round(iCountTimecards / 200) + 1;
    
SYSTEM.DEBUG('+++++ iNumberOfBatches: ' + iNumberOfBatches);

	reqWorkspaces.setTimeout(120000);
    
	for(Integer iBatchNum = 1; iBatchNum <= iNumberOfBatches; iBatchNum++) 
    {

		reqWorkspaces.setEndpoint('https://api.mavenlink.com/api/v1/time_entries.json'+
                                  '?created_after=2015-08-01T000000&created_before=2015-16-01T000000&page=' + iBatchNum );
	
		resWorkspaces = new http().send(reqWorkspaces);
		
		JSONParser parser = JSON.createParser(resWorkspaces.getBody());
        
		String strTCID = '';
		String strProjectID = '';
		String strUserID = '';
		String strApproved = '';
		String strBillable = '';
		String strCreatedAt = ''; 
		String strUpdatedAt = '';
		String strDatePerformed = '';
		String strTimeInMinutes = '';
		String strRateInCents = '';
		String strNotes = '';
		String strStoryID = '';
        
		while(parser.nextToken() != null) 
		{
		    if(parser.getCurrentToken() == JSONToken.FIELD_NAME) 
		    {
                if(parser.getText() == 'created_at')
                {
                    parser.nextToken();
                    strCreatedAt = parser.getText();
                }
                
                if(parser.getText() == 'updated_at')
                {
                    parser.nextToken();
                    strUpdatedAt = parser.getText();
                }
                
                if(parser.getText() == 'date_performed')
                {
                    parser.nextToken();
                    strDatePerformed = parser.getText();
                }
                
                if(parser.getText() == 'time_in_minutes')
                {
                    parser.nextToken();
                    strTimeInMinutes = parser.getText();
                }
                
                if(parser.getText() == 'billable')
                {
                    parser.nextToken();
                    strBillable = parser.getText();
                }
                
                if(parser.getText() == 'notes')
                {
                    parser.nextToken();
                    strNotes = parser.getText();
                }
                
                if(parser.getText() == 'rate_in_cents')
                {
                    parser.nextToken();
                    strRateInCents = parser.getText();
                }
                
                if(parser.getText() == 'approved')
                {
                    parser.nextToken();
                    strApproved = parser.getText();
                }
                
                if(parser.getText() == 'story_id')
                {
                    parser.nextToken();
                    strStoryID = parser.getText();
                }
                
                if(parser.getText() == 'workspace_id')
                {
                    parser.nextToken();
		        	strProjectID = parser.getText();
                }
		        		    		    
                if(parser.getText() == 'user_id') 
                {
                    parser.nextToken();
                    strUserID = parser.getText();
                }
                
 // Commenting out the next 'if' block causes a successful execution... leaving it as is
 // causes an unknown failure
                
                if(parser.getText() == 'id')
                {
                    parser.nextToken();
                    strTCID = parser.getText();
                    
                    mapKeyValue.put('id', strTCID);
                    mapKeyValue.put('workspace_id', strProjectID);
                    mapKeyValue.put('created_at', strCreatedAt);
                    mapKeyValue.put('updated_at', strUpdatedAt);
                    mapKeyValue.put('date_performed', strDatePerformed);
                    mapKeyValue.put('time_in_minutes', strTimeInMinutes);
                    mapKeyValue.put('billable', strBillable);
                    mapKeyValue.put('notes', strNotes);
                    mapKeyValue.put('rate_in_cents', strRateInCents);
                    mapKeyValue.put('approved', strApproved);
                    mapKeyValue.put('story_id', strStoryID);
                    mapKeyValue.put('user_id', strUserID);
                    
                    mapJSON.put(strTCID, mapKeyValue);
                    mapKeyValue.clear();
                    
                }

                
            } // if(parser.getCurrentToken() == JSONToken.FIELD_NAME) 

		} // while(parser.nextToken() != null) 
        
	} // for(Integer iBatchNum = 1; iBatchNum <= iNumberOfBatches; iBatchNum++) 
} // if(iCountTimecards > 0)

 
VinodKRVinodKR
Hi David,

JSON Deserialization Techniques is the ideal way,change your code as per the link which explains things step by step.

Link - http://opfocus.com/blog/json-deserialization-techniques-in-salesforce/

Thanks,

Have a great day ahead,Let the Force be with you!
Please mark this as best answer if it helps you.
David BermanDavid Berman
Hi Vinod

i don't think serialization will work, since the JSON is badly formatted. See here an example of what it looks like: 

https://developer.salesforce.com/forums/ForumsMain?id=906F0000000MLmYIAW

If you can think of a way of serializing this, I would greatly appreciate it.