You need to sign in to do that
Don't have an account?
María Jesús Carmona
Deserialize JSON to a list
Hi!
I would like to know how to deserialize a JSON response on a list (to use it later on a for).
Im trying to deserialize the response, but the result of the deserialize is always null.
The response I receive contains a list of ServiceNow incidents, on the following format:
{"result":[{"short_description":"text","sys_id":"004fbc4cdb062300d6e781cc0b961989","contact_type":"email","incident_state":"2","impact":"4","description":"text"},{"short_description":"text","sys_id":"0188096ddb45a340d6e781cc0b961971","contact_type":"email","incident_state":"2","impact":"4","description":"text"},{"short_description":"text","sys_id":"02786099db66df40d6e781cc0b96197a","contact_type":"email","incident_state":"2","impact":"4","description":"text"}]}
At the moment my code looks like this:
ServiceNowBatch
CaseWrapper
CaseWrapperList
I don't know what I'm missing.
I would like to know how to deserialize a JSON response on a list (to use it later on a for).
Im trying to deserialize the response, but the result of the deserialize is always null.
The response I receive contains a list of ServiceNow incidents, on the following format:
{"result":[{"short_description":"text","sys_id":"004fbc4cdb062300d6e781cc0b961989","contact_type":"email","incident_state":"2","impact":"4","description":"text"},{"short_description":"text","sys_id":"0188096ddb45a340d6e781cc0b961971","contact_type":"email","incident_state":"2","impact":"4","description":"text"},{"short_description":"text","sys_id":"02786099db66df40d6e781cc0b96197a","contact_type":"email","incident_state":"2","impact":"4","description":"text"}]}
At the moment my code looks like this:
ServiceNowBatch
//Authentication + request done (req and res) // (...) List<Case> casesToUpdate = new List<Case>; String response = res.getBody(); //Having problems here CaseWrapper caseWrapperDes = (CaseWrapper)JSON.deserialize(response, CaseWrapper.class); System.debug('caseWrapper content: '+caseWrapperDes); // (...) for (CaseWrapper SNcase : caseWrapperDes) { if (response.length() > 0 && response.substring(0, 5) != 'Error') { Case c = new Case(); c.description = SNcase.description; c.origin = SNcase.contact_type; casesToUpdate.add(c); } } return casesToUpdate; (...)
CaseWrapper
CaseWrapper public class CaseWrapper { public List<resultSN> results {get; set;} public class resultSN { public String short_description {get; set;} public String sys_id {get; set;} public String contact_type {get; set;} public String incident_state {get; set;} public String impact {get; set;} public String description {get; set;} } }
CaseWrapperList
CaseWrapperList public class CaseWrapperList { public List<CaseWrapper> results; }
I don't know what I'm missing.
For starters, your JSON says the field name is "result" but in your CaseWrapper class it's called "results".
Also, line 11
should probably be
It's also against convention to start class names like "resultSN" with a lowercase letter, although it will still run just fine.
Lastly, I would move the if statement out of the loop. The condition does not depend on your loop variable SNcase, it will be either true for all of them or false for all of them. This just wastes CPU cycles. You should do the check once, and if it's true then enter your code block