1

I have integrated one API and I am getting response in one variable string by Class A.apxc

String response = res.getBody();

I have one parser class of the response

public class GetTransactionContainer{   
    public cls_transaction[] transactions;    
    class cls_transaction {        
        public String CONTAINER;
        public Integer id;  
        public String type;  
        public String subType;  
        public cls_amount amount;
        public String baseType;  
        public String categoryType; 
        public Integer categoryId; 
        public String category;  
        public String categorySource; 
        public cls_description description;
        //public String date;  
        public String postDate;  
        public String transactionDate; 
        public boolean isManual;
        public String status;  
        public Integer accountId;  
        public cls_runningBalance runningBalance;
        public String checkNumber;  
        public Integer highLevelCategoryId;  
    }
    class cls_amount {
        public Integer amount;  
        public String currencyAmount;  
    }
    class cls_description {
        public String original;
        public String simple;  
    }
    class cls_runningBalance {
        public Integer amount;  
        public String currencyAmount;  
    }
    public Static GetTransactionContainer parse(String json){

        return (GetTransactionContainer) System.JSON.deserialize(json,GetTransactionContainer.class);
    }    
}

I want to get the values from GetTransactionContainer class and put it into Object Named Transaction__c. Whenever I am trying to do

GetTransactionContainer getTxn = GetTransactionContainer.parse(response);

I am not getting anything in getTxn even if there is response in "response" variable. I would be thankful if anybody can help me on this. Not sure if I am doing something wrong or missing?

Sample Response

{
    "transaction": [
        {
            "CONTAINER": "bank",
            "id": 25335410,
            "type": "OTHER_DEPOSITS",
            "subType": "CREDIT",
            "amount": {
                "amount": 3465,
                "currency": "USD"
            },
            "baseType": "CREDIT",
            "categoryType": "INCOME",
            "categoryId": 32,
            "category": "Other Income",
            "categorySource": "SYSTEM",
            "description": {
                "original": "DESC",
                "simple": "DESC"
            },
            "date": "2017-03-16",
            "postDate": "2017-03-16",
            "transactionDate": "2017-03-16",
            "isManual": false,
            "status": "POSTED",
            "accountId": 10563602,
            "runningBalance": {
                "amount": 59,
                "currency": "USD"
            },
            "checkNumber": "DAG-133",
            "highLevelCategoryId": 10000012
        },
        {
            "CONTAINER": "bank",
            "id": 25335412,
            "type": "OTHER_WITHDRAWALS",
            "subType": "DEBIT",
            "amount": {
                "amount": 3103,
                "currency": "USD"
            },
            "baseType": "DEBIT",
            "categoryType": "EXPENSE",
            "categoryId": 19,
            "category": "Other Expenses",
            "categorySource": "SYSTEM",
            "description": {
                "original": "DESC",
                "simple": "DESC"
            },
            "date": "2017-03-14",
            "postDate": "2017-03-14",
            "transactionDate": "2017-03-12",
            "isManual": false,
            "status": "POSTED",
            "accountId": 10563602,
            "runningBalance": {
                "amount": 59,
                "currency": "USD"
            },
            "checkNumber": "DAG-333",
            "highLevelCategoryId": 10000009
        },
        {
            "CONTAINER": "bank",
            "id": 25335411,
            "type": "OTHER_DEPOSITS",
            "subType": "CREDIT",
            "amount": {
                "amount": 5646,
                "currency": "USD"
            },
            "baseType": "CREDIT",
            "categoryType": "INCOME",
            "categoryId": 32,
            "category": "Other Income",
            "categorySource": "SYSTEM",
            "description": {
                "original": "DESC",
                "simple": "DESC"
            },
            "date": "2017-03-10",
            "postDate": "2017-03-10",
            "transactionDate": "2017-03-11",
            "isManual": false,
            "status": "POSTED",
            "accountId": 10563602,
            "runningBalance": {
                "amount": 59,
                "currency": "USD"
            },
            "checkNumber": "DAG-233",
            "highLevelCategoryId": 10000012
        },
        {
            "CONTAINER": "bank",
            "id": 25335413,
            "type": "OTHER_DEPOSITS",
            "subType": "CREDIT",
            "amount": {
                "amount": 9846,
                "currency": "USD"
            },
            "baseType": "CREDIT",
            "categoryType": "INCOME",
            "categoryId": 32,
            "category": "Other Income",
            "categorySource": "SYSTEM",
            "description": {
                "original": "DESC",
                "simple": "DESC"
            },
            "date": "2017-03-02",
            "postDate": "2017-03-02",
            "transactionDate": "2017-03-16",
            "isManual": false,
            "status": "POSTED",
            "accountId": 10563602,
            "runningBalance": {
                "amount": 59,
                "currency": "USD"
            },
            "checkNumber": "DAG-333",
            "highLevelCategoryId": 10000012
        }
    ]
}
  • can you post JSON returned? – kurunve Jun 19 '17 at 8:34
  • 4
    Check that the JSON text in the response variable matches the class. For example does the JSON start with {"transactions": [... or [.... If it is the latter, you probably need JSON.deserialize(json,cls_transaction[].class);. json2apex.herokuapp.com is very helpful for getting the Apex and JSON to match. – Keith C Jun 19 '17 at 8:35
  • 2
    @Sagar The JSON has "transaction" but you've called the field "transactions": change the field name. (Or if you are in control of the JSON generation, using a plural in the JSON reads a bit better so change at that side.) Your existing JSON.deserialize call is correct. – Keith C Jun 19 '17 at 9:59
  • 1
    Thanks @KeithC. yes I saw that. I changed the name for transaction as it is keyword. I edited the response only. now it is working as expected. – Sagar Jun 19 '17 at 10:03
  • 2
    That's good. Go ahead and post that as an answer yourself so people know the problem is solved. (If your reputation points allow you to do that.) Then you can accept that answer in a couple of days. – Keith C Jun 19 '17 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.