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
Katharine Anderson 13Katharine Anderson 13 

Issues parsing JSON response from Box

I'm new to coding and I'm having trouble figuring out how to parse the JSON response from Box. What I want to do is to do a callout to box, and then return a list of the items (files) in a folder and then print that list into a visualforce page. I it to show like a related list with the name of the item, the name of the person who uploaded it, and the date that they uploaded it. I also want to filter the list to only show the ones that have been uploaded in the last year.

I found this code in the Box SDK for Salesforce that is their JSON Parser, but I am not sure that I understand it well enough to try to use it: https://github.com/box/box-salesforce-sdk/blob/fd7d28f48fe2a498b08ab2c926433d422e89b30e/src/classes/BoxJsonObject.cls#L170

Can you help me figure out if this is useful for me and how to plug it into my code if so? Currently, when I try, I get this error: EXCEPTION_THROWN [29]|BoxJsonObject.BoxJsonObjectException: Json passed to BoxJsonObject.parseJsonStringArray must start with JSONToken.START_ARRAY

Here is the code that I am currently using that returns the error. Ideally, I would do the callout to Box when the Visualforce page is rendered and then I wouldn't have to store the list in Salesforce and update it before rendering the visualforce page, but I'm not sure if that's possible:
global with sharing class GetMonitoringDocuments {
//instantiate controller
  public GetMonitoringDocuments(ApexPages.StandardController controller){}
//declare variables
  public String folderId {get;set;}
  public String piId {get;set;}
  public String aiId {get;set;}
  public List<ID> ids = new List<ID> {};
//future necessary because it's an http callout to Box using Named Credentials
//Not sure how to filter the list to only show items/files that were uploaded in the monitoring year
@future (callout=true)  
public static void getFolderItems(ID aiId, ID piId, String folderId){
	Http http = new Http();
	HttpRequest req = new HttpRequest();
	req.setMethod('GET');
	req.setEndpoint('callout:BoxForSalesforce/folders/' + folderId + '/items?fields=name,created_at,created_by,shared_link');
            
	HttpResponse response = http.send(req);
            
	system.debug('HttpResponse=>' + response);
//lookup annual inspection record to update
	Annual_Inspection__c AI = [SELECT ID, Monitoring_Documentation__c, Update_Monitoring_Documentation__c
                              FROM Annual_Inspection__c 
                              WHERE ID =: aiId];
 // Update the folder monitoring documentation field with the Parsed JSON (currently getting an error: EXCEPTION_THROWN [29]|BoxJsonObject.BoxJsonObjectException: Json passed to BoxJsonObject.parseJsonStringArray must start with JSONToken.START_ARRAY) 
 //Ideally, rather than putting the parsed JSON into a field, I would want to make the callout when the visualforce page is rendered and then just show it there rather than storing the list in Salesforce.
    BoxJsonObject json = new BoxJsonObject();
    list<String> parsedJSON = json.parseJsonStringArray(response.getBody());
    parsedJson.remove(null);
    AI.Monitoring_Documentation__c = string.join(parsedJSON,'BR()');
    AI.Update_Monitoring_Documentation__c = False;

//Commit the changes.
    update AI;
    
}
//Make it an invocable method so that it can be called by process builder. The code is then run when a checkbox is checked on the record.
@InvocableMethod(label='Get Folder Items' description='Returns the list of folder items.')
  public static void getAIIDs(List<ID> ids) {
    List<Annual_Inspection__c> AIlist = [SELECT id, property_interest__c, Property_Interest__r.MDFolderId__c FROM Annual_Inspection__c WHERE Id in :ids];
    for (Annual_Inspection__c AIs : AIlist) {
      GetMonitoringDocuments.getFolderItems(AIs.id, AIs.property_interest__c, AIs.Property_Interest__r.MDFolderId__c);
    }
  } 
}

Oh and just in case it's useful, here's the unparsed JSON response that I'm working with (with IDs and actual email addresses redacted):
{"total_count":3,"entries":[{"type":"file","id":"230638187456","etag":"0","name":"Preserve preliminary monitoring report 2016.docx","created_at":"2017-09-27T16:16:20-07:00","created_by":{"type":"user","id":"xxxxxxxxxxx","name":"Tester Katharine Anderson","login":"xxx@gmail.com"},"shared_link":null},{"type":"file","id":"xxxxxxxxxxx","etag":"0","name":"Preserve site visit 28 August 2015.docx","created_at":"2017-09-27T16:16:19-07:00","created_by":{"type":"user","id":"xxxxxxxxxxx","name":"Tester Katharine Anderson","login":"xxx@gmail.com"},"shared_link":null},{"type":"file","id":"xxxxxxxxxxx","etag":"0","name":"Posting Authorship pre-2018.csv","created_at":"2017-09-27T16:16:19-07:00","created_by":{"type":"user","id":"xxxxxxxxxxx","name":"Tester Katharine Anderson","login":"xxx@gmail.com"},"shared_link":null}],"offset":0,"limit":100,"order":[{"by":"type","direction":"ASC"},{"by":"name","direction":"ASC"}]}