[SalesForce] Deserialize JSON to Map – EchoSign REST API response help

I am making a call to the EchoSign REST API and I need to get the signing URL of the most recently signed document using an EchoSign query. I am using the following:

HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.echosign.com:443/api/rest/v3/agreements?query=Agreement%20for%20APEX%20Test%20Account%20-%202015-04-02');
req.setMethod('GET');
req.setHeader('Access-Token', OAuthToken);
req.setHeader('Content-Type', 'application/json');
req.setHeader('Charset', 'UTF-8');

HttpResponse res = new HttpResponse();
Http http = new Http();
res = http.send(req);
System.debug(res.getBody());

The issue is due to the name format of the docs, if several are sent in a day from the same account they will all have the same name generated automatically for them…

So I am trying to get the body of the response and work out which agreement was sent at which time according to the EchoSign system and match this up to the agreements that I have to work out which ID matched which Agreement.

JSON Response:

{
  "userAgreementList": [
    {
      "displayDate": "2015-04-02T09:32:23-07:00",
      "displayUserInfo": {
        "company": "TEST",
        "fullNameOrEmail": "Tim 1234"
      },
      "esign": true,
      "agreementId": "AGREEMENT ID",
      "latestVersionId": "VERSION ID",
      "name": "Agreement for APEX Test Account - 2015-04-02",
      "status": "OUT_FOR_SIGNATURE"
    },
    {
      "displayDate": "2015-04-02T03:32:57-07:00",
      "displayUserInfo": {
        "company": "TEST",
        "fullNameOrEmail": "Tim 1234"
      },
      "esign": true,
      "agreementId": "AGREEMENT ID",
      "latestVersionId": "VERSION ID",
      "name": "Agreement for APEX Test Account - 2015-04-02",
      "status": "OUT_FOR_SIGNATURE"
    },
    {
      "displayDate": "2015-03-31T09:15:11-07:00",
      "displayUserInfo": {
        "company": "TEST",
        "fullNameOrEmail": "Tim 1234"
      },
      "esign": true,
      "agreementId": "AGREEMENT ID",
      "latestVersionId": "VERSION ID",
      "name": "Agreement for APEX Test Account - 2015-04-02",
      "status": "RECALLED"
    }
  ]
}

I can't for the life of me work out how to convert the JSON into something that is readable… I have tried using JSON.deserialize and JSON.deserializeuntyped however I am getting errors constantly with both. I managed to get the JSON.deserializeuntyped working, however I could only get it to work when I converted this to to a Map of string and objects (Map<String, Object>), but I can't work with this data how I want to be able to work with it :/

Best Answer

As sfdcfox suggested in a comment, you can use json2apex to generate the necessary code from a JSON sample. The app is a little out of date in that test methods should now be in a different class, so I generated the code and separated out the test method:

UserAgreements Class

public class UserAgreements {
    public class DisplayUserInfo {
        public String company;
        public String fullNameOrEmail;
    }

    public List<UserAgreementList> userAgreementList;

    public class UserAgreementList {
        public String displayDate;
        public DisplayUserInfo displayUserInfo;
        public Boolean esign;
        public String agreementId;
        public String latestVersionId;
        public String name;
        public String status;
    }


    public static UserAgreements parse(String json) {
        return (UserAgreements) System.JSON.deserialize(json, UserAgreements.class);
    }   
}

UserAgreementsTest Class

@isTest
public class UserAgreementsTest {
    static testMethod void testParse() {
        String json = '{'+
        '  \"userAgreementList\": ['+
        '    {'+
        '      \"displayDate\": \"2015-04-02T09:32:23-07:00\",'+
        '      \"displayUserInfo\": {'+
        '        \"company\": \"TEST\",'+
        '        \"fullNameOrEmail\": \"Tim 1234\"'+
        '      },'+
        '      \"esign\": true,'+
        '      \"agreementId\": \"AGREEMENT ID\",'+
        '      \"latestVersionId\": \"VERSION ID\",'+
        '      \"name\": \"Agreement for APEX Test Account - 2015-04-02\",'+
        '      \"status\": \"OUT_FOR_SIGNATURE\"'+
        '    },'+
        '    {'+
        '      \"displayDate\": \"2015-04-02T03:32:57-07:00\",'+
        '      \"displayUserInfo\": {'+
        '        \"company\": \"TEST\",'+
        '        \"fullNameOrEmail\": \"Tim 1234\"'+
        '      },'+
        '      \"esign\": true,'+
        '      \"agreementId\": \"AGREEMENT ID\",'+
        '      \"latestVersionId\": \"VERSION ID\",'+
        '      \"name\": \"Agreement for APEX Test Account - 2015-04-02\",'+
        '      \"status\": \"OUT_FOR_SIGNATURE\"'+
        '    },'+
        '    {'+
        '      \"displayDate\": \"2015-03-31T09:15:11-07:00\",'+
        '      \"displayUserInfo\": {'+
        '        \"company\": \"TEST\",'+
        '        \"fullNameOrEmail\": \"Tim 1234\"'+
        '      },'+
        '      \"esign\": true,'+
        '      \"agreementId\": \"AGREEMENT ID\",'+
        '      \"latestVersionId\": \"VERSION ID\",'+
        '      \"name\": \"Agreement for APEX Test Account - 2015-04-02\",'+
        '      \"status\": \"RECALLED\"'+
        '    }'+
        '  ]'+
        '}';
        UserAgreements obj = UserAgreements.parse(json);
        System.assert(obj != null);
    }
}
Related Topic