0

I am not able fetch all details for message & text fields node. Getting error as:

Message consuming unrecognized property: name, Message consuming unrecognized property: userId, Message consuming unrecognized property: items, Message consuming unrecognized property: text, Message consuming unrecognised .

Here is my json:

{  
  "messages":[  
    {  
      "type":"ChatEstablished",
      "message":{  
        "name":"Customer Support",
        "userId":"0056A000000ivth",
        "items":[  

        ],
        "sneakPeekEnabled":true,
        "chasitorIdleTimeout":{  
          "isEnabled":false
        }
      }
    },
    {  
      "type":"ChatMessage",
      "message":{  
        "text":"Welcome to Wyndham chat service. How can I help you?",
        "name":"Customer Support",
        "schedule":{  
          "responseDelayMilliseconds":0.0
        },
        "agentId":"0056A000000ivth"
      }
    }
  ],
  "sequence":2,
  "offset":1401052138
}

I have created a apex class through https://json2apex.herokuapp.com. Here is the code of apex class.

public class Messages {
    public String type_Z {get;set;} // in json: type
    public Message message {get;set;} 

    public Messages(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (text == 'type') {
                        type_Z = parser.getText();
                    } else if (text == 'message') {
                        message = new Message(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'Messages consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

public class Message {
    public List<CustomDetails> customDetails {get;set;} 
    public Integer connectionTimeout {get;set;} 
    public String visitorId {get;set;} 
    public List<CustomDetails> sensitiveDataRules {get;set;} 
    public Boolean transcriptSaveEnabled {get;set;} 
    public String url {get;set;} 
    public Integer queuePosition {get;set;} 
    public GeoLocation geoLocation {get;set;} 

    public Message(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (text == 'customDetails') {
                        customDetails = arrayOfCustomDetails(parser);
                    } else if (text == 'connectionTimeout') {
                        connectionTimeout = parser.getIntegerValue();
                    } else if (text == 'visitorId') {
                        visitorId = parser.getText();
                    } else if (text == 'sensitiveDataRules') {
                        sensitiveDataRules = arrayOfCustomDetails(parser);
                    } else if (text == 'transcriptSaveEnabled') {
                        transcriptSaveEnabled = parser.getBooleanValue();
                    } else if (text == 'url') {
                        url = parser.getText();
                    } else if (text == 'queuePosition') {
                        queuePosition = parser.getIntegerValue();
                    } else if (text == 'geoLocation') {
                        geoLocation = new GeoLocation(parser);
                    } else {
                        System.debug(LoggingLevel.WARN, 'Message consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

public List<Messages> messages {get;set;} 
public Integer sequence {get;set;} 

public LiveAgentJsonParser(JSONParser parser) {
    while (parser.nextToken() != System.JSONToken.END_OBJECT) {
        if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
            String text = parser.getText();
            if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                if (text == 'messages') {
                    messages = arrayOfMessages(parser);
                } else if (text == 'sequence') {
                    sequence = parser.getIntegerValue();
                } else {
                    System.debug(LoggingLevel.WARN, 'LiveAgentJsonParser consuming unrecognized property: '+text);
                    consumeObject(parser);
                }
            }
        }
    }
}

public class GeoLocation {
    public String organization {get;set;} 
    public String region {get;set;} 
    public String city {get;set;} 
    public String countryName {get;set;} 
    public Double latitude {get;set;} 
    public String countryCode {get;set;} 
    public Double longitude {get;set;} 

    public GeoLocation(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    if (text == 'organization') {
                        organization = parser.getText();
                    } else if (text == 'region') {
                        region = parser.getText();
                    } else if (text == 'city') {
                        city = parser.getText();
                    } else if (text == 'countryName') {
                        countryName = parser.getText();
                    } else if (text == 'latitude') {
                        latitude = parser.getDoubleValue();
                    } else if (text == 'countryCode') {
                        countryCode = parser.getText();
                    } else if (text == 'longitude') {
                        longitude = parser.getDoubleValue();
                    } else {
                        System.debug(LoggingLevel.WARN, 'GeoLocation consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}

public class CustomDetails {

    public CustomDetails(JSONParser parser) {
        while (parser.nextToken() != System.JSONToken.END_OBJECT) {
            if (parser.getCurrentToken() == System.JSONToken.FIELD_NAME) {
                String text = parser.getText();
                if (parser.nextToken() != System.JSONToken.VALUE_NULL) {
                    {
                        System.debug(LoggingLevel.WARN, 'CustomDetails consuming unrecognized property: '+text);
                        consumeObject(parser);
                    }
                }
            }
        }
    }
}


public static LiveAgentJsonParser parse(String json) {
    System.JSONParser parser = System.JSON.createParser(json);
    return new LiveAgentJsonParser(parser);
}

public static void consumeObject(System.JSONParser parser) {
    Integer depth = 0;
    do {
        System.JSONToken curr = parser.getCurrentToken();
        if (curr == System.JSONToken.START_OBJECT || 
            curr == System.JSONToken.START_ARRAY) {
            depth++;
        } else if (curr == System.JSONToken.END_OBJECT ||
            curr == System.JSONToken.END_ARRAY) {
            depth--;
        }
    } while (depth > 0 && parser.nextToken() != null);
}



private static List<Messages> arrayOfMessages(System.JSONParser p) {
    List<Messages> res = new List<Messages>();
    if (p.getCurrentToken() == null) p.nextToken();
    while (p.nextToken() != System.JSONToken.END_ARRAY) {
        res.add(new Messages(p));
    }
    return res;
}








private static List<CustomDetails> arrayOfCustomDetails(System.JSONParser p) {
    List<CustomDetails> res = new List<CustomDetails>();
    if (p.getCurrentToken() == null) p.nextToken();
    while (p.nextToken() != System.JSONToken.END_ARRAY) {
        res.add(new CustomDetails(p));
    }
    return res;
}
}

Thanks in advance

1
  • 4
    Hi Neetu - please take a moment to format your json and code using the edit link and use the {} to do the formatting
    – cropredy
    Commented Feb 9, 2019 at 0:07

1 Answer 1

1

At first blush, this looks way too complicated and I would try a simpler approach with a class structure that looks like

public class MessageWrapper {
  Message[] messages;
  Integer sequence;
  Long offset;

public class Message {
   String type_Z;
   MessageDetail message;
}

public class MessageDetail {
  String name;
  String text;
  String userId;
  Item[] items;
  Boolean sneakPeekEnabled;
  ChasitorIdleTimeOut chasitorIdleTimeout;
  Schedule schedule;
  ID agentId;
}

public class Item {    
}

public class Schedule{
}

public class ChasitorIdleTimeOut {
}

and then simply deserialize the incoming json into

MessageWrapper mw = (MessageWrapper) Json.deserialize(myJson,MessageWrapper.class);
1
  • Sure, but the point of JSON2Apex is to not to have to do this. Commented Sep 30, 2021 at 19:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .