I have reviewed a lot of the other deserialize issues and I keep running into problems. What I have been able to gather is that if I have a response coming back with a reserved word in the JSON, I can either use the deserializeUntyped methodology or use JSON parser. I tried using JSON2Apex and was hitting the cpu limit even with next to nothing in the response. So I reverted to my Untyped solution and keep running into a type casting error as follows:
20:17:33:209 FATAL_ERROR System.TypeException: Invalid conversion from runtime type String to List
It is pointing to the first line of the first for loop in the following code. For some reason it is treating the return of 'result' which is an array of objects as a string? Very confusing. Postman response further down
// Hoping there is a better way to do this than using Object everywhere
Map<String, Object> deserialized = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
List<Map<String, Object>> results = new List<Map<String, Object>>();
List<Opportunity> opportunitiesToInsert = new List<Opportunity>();
for(Object instance : (List<Object>)deserialized.get('result')) {
results.add((Map<String, Object>)instance);
}
for(Map<String, Object> result : results) {
Opportunity opp = new Opportunity();
opp.Block_Number__c = (Integer) Integer.valueOf(result.get('blockNumber'));
opp.Time_Stamp__c = (Datetime) Datetime.newInstance(Long.valueOf((String) result.get('timeStamp')));
opp.Hash__c = (String) result.get('hash');
opp.Nonce__c = (Integer) Integer.valueOf(result.get('nonce'));
opp.Block_Hash__c = (String) result.get('blockHash');
opp.Transaction_Index__c = (Integer) Integer.valueOf(result.get('nonce'));
opp.From_Address__c = (String) result.get('from');
opp.To_Address__c = (String) result.get('to');
opp.Transaction_Amount__c = (Decimal) toEth((String) result.get('value'));
opp.Gas_Price_in_Eth__c = (Decimal) toEth((String) result.get('gasPrice'));
opp.isError__c = (Boolean) Boolean.valueOf(result.get('isError'));
opp.Transaction_Rec__c = (Boolean) Boolean.valueOf(result.get('txreceipt_status'));
opp.Input__c = (String) result.get('input');
opp.Contract_Address__c = (String) result.get('contractAddress');
opp.Cumulative_Gas_Used__c = (Integer) Integer.valueOf(result.get('cumulativeGasUsed'));
opp.Gas_Used__c = (Integer) Integer.valueOf(result.get('gasUsed'));
opp.Confirmations__c = (Integer) Integer.valueOf(result.get('confirmations'));
opp.AccountId = accountId;
opportunitiesToInsert.add(opp);
}
{
"status": "1",
"message": "OK",
"result": [
{
"blockNumber": "10184431",
"timeStamp": "1591074958",
"hash": "0xbf3caa100dc0b9f46e15a940287a0e44b78cff06d0eef8a8d4d5e295a919bbf2",
"nonce": "462",
"blockHash": "0xb6380bd0c92c33a17208af6f43192ce9e580db4eb9278fd1507706650165ed1d",
"transactionIndex": "9",
"from": "0x88d34944cf554e9cccf4a24292d891f620e9c94f",
"to": "0x558553d54183a8542f7832742e7b4ba9c33aa1e6",
"value": "1000000000000000000",
"gas": "21000",
"gasPrice": "50000000000",
"isError": "0",
"txreceipt_status": "1",
"input": "0x",
"contractAddress": "",
"cumulativeGasUsed": "340221",
"gasUsed": "21000",
"confirmations": "55156"
},
{
"blockNumber": "10184559",
"timeStamp": "1591076771",
"hash": "0xa5fe1b6b438a9d9a8f68f384f5b86814a1466464749f7681dd37a1aafb5e0161",
"nonce": "0",
"blockHash": "0xbb98e5ab911efed60bf3375276f0a7bd2c1f5ca144d16d9037c82f9409f6855d",
"transactionIndex": "2",
"from": "0x558553d54183a8542f7832742e7b4ba9c33aa1e6",
"to": "0xefb2e870b14d7e555a31b392541acf002dae6ae9",
"value": "998866000000000000",
"gas": "21000",
"gasPrice": "54000000000",
"isError": "0",
"txreceipt_status": "1",
"input": "0x",
"contractAddress": "",
"cumulativeGasUsed": "85715",
"gasUsed": "21000",
"confirmations": "55028"
},
{
"blockNumber": "10184571",
"timeStamp": "1591076943",
"hash": "0xc931dbb3c6de9df1b50d48026015275236de06b5fd4fa22f0c2921cd755cebc1",
"nonce": "1",
"blockHash": "0x2cfab75b0245a90d8aafc61ef22afd835bff0022ca6b2b03b3cda47f1ebad1e1",
"transactionIndex": "21",
"from": "0xd273bd546b11bd60214a2f9d71f22a088aafe31b",
"to": "0x558553d54183a8542f7832742e7b4ba9c33aa1e6",
"value": "400001000000000000000000",
"gas": "21000",
"gasPrice": "54000000000",
"isError": "0",
"txreceipt_status": "1",
"input": "0x",
"contractAddress": "",
"cumulativeGasUsed": "862453",
"gasUsed": "21000",
"confirmations": "55016"
},
{
"blockNumber": "10220711",
"timeStamp": "1591560252",
"hash": "0x3a9634bd9fb68fa9aa550ddadda5f7e034684a8c957deb7be4832c2d28d44b52",
"nonce": "26192",
"blockHash": "0x7485a3f3ac312d7c0746f7ed625a0e78aed11c249a83b31a4c80f4c2c50a4085",
"transactionIndex": "103",
"from": "0x491fe5f4724e642c90372b0b95b60c4ac8d13f1c",
"to": "0x558553d54183a8542f7832742e7b4ba9c33aa1e6",
"value": "1000000000000000",
"gas": "21000",
"gasPrice": "31824000000",
"isError": "0",
"txreceipt_status": "1",
"input": "0x",
"contractAddress": "",
"cumulativeGasUsed": "5494053",
"gasUsed": "21000",
"confirmations": "18876"
}
]
}
I've gone back and forth on this quite a bit with the latest attempt being the first for loop you see above in an effort to properly cast the list of transactions.