I an trying to deserialize a Json string to use in my callout but get the error Invalid conversion from runtime type List<ANY> to List<or_guestsJSON>


HttpResponse response = http.send(request);
        if (response.getStatusCode() == 200) {
           List<or_leadsJSON> input = (List<or_leadsJSON>)JSON.deserializeUntyped(response.getBody());

Class object

public class or_leadsJSON{
    class cls_0 {
        public String city; //Irvine
        public String state;    //California
    class cls_agency {
        public String uid;  //0000-0000-0000-0000-0000
        public String name; //Responseev
    class cls_property {
        public String uid;  //0000-0000-0000-0000-0000
        public boolean isActive;
        public String currencyRep; //USD
    class cls_photos {
        public String url;  //https://www.orbirental.com/img/uploader/view_0.jpg
        public String description;  //my house
    class cls_stayDetails {
        public String departureDate;    //2017-02-25 11:00:00.0
        public String arrivalDate;  //2017-02-24 15:00:00.0
        public String extraNotes;   //Some notes
    public static or_leadsJSON parse(String json){
        String jsonReplace =  json.replace('"currency":', '"currencyRep":');
        return (or_leadsJSON) System.JSON.deserialize(jsonReplace, or_leadsJSON.class);

Change this:

List<or_guestsJSON> input = (List<or_guestsJSON>) JSON.deserializeUntyped(

to this:

List<or_guestsJSON> input = (List<or_guestsJSON>) JSON.deserialize(

so that the parser has the type information it needs to create the correct type which in this case is a list.

(For arrays deserializeUntyped always creates the most generic collection type of List<Object>. You can still work with that, but you need additional code to convert to the correct type of list so better to use deserialize when possible.)

  • I have edited my question with leads instead of guests. When I debug this I get : input(or_leadsJSON:[], or_leadsJSON:[], or_leadsJSON:[], or_leadsJSON:[], or_leadsJSON:[], or_leadsJSON:[], or_leadsJSON:[], or_leadsJSON:[]) – Thomas Oct 24 '18 at 12:18
  • @Thomas Not to sure what you mean here. Bottom line is that if the JSON has a fixed structure use deserialize with an explicit type (class). – Keith C Oct 24 '18 at 13:00
  • In this case isnt my or_leadsJSON empty or how do I get cls_stayDetails arrivalDate? – Thomas Oct 24 '18 at 13:25
  • @Thomas Best you add a sample of the JSON that fails to the question. Your class or_leadsJSON is entirely missing any properties which would give you the result you see. If you are trying to support multiple formats in one piece of code, you will need to use deserializeUntyped and handle the differences using explicit map-access code. – Keith C Oct 24 '18 at 13:30

if you know a structure of JSON and already have created wrapper to store deserialized result - use deserialize method with type as second param of method

or_guestsJSON input = JSON.deserialize(jsonReplace, or_leadsJSON.class);

you already have parse method, as JSON2Apex generates it as well (looks like you used this tool)

if (response.getStatusCode() == 200) {
    or_guestsJSON input = or_guestsJSON.parse(response.getBody());

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.