[SalesForce] Parse JSON with Apex

I am trying to deserialize below JSON . Problem is that sometimes message is returned as an object and sometimes as an array .

In this JSON , I have combined three HTTP responses into one so as to get a complete set. It is a valid json per JSONLint.

{
  "LineStatus": [
    {
      "line": "1.00",
      "ConfigStatus": "E",
      "ConfiguratorMessages": {
        "message": {
          "MessageType": "E",
          "message": "PRODUCT REQUIRED."
        }
      }
    },
    {
      "line": "2.00",
      "ConfigStatus": ""
    },
    {
      "line": "3.00",
      "ConfigStatus": "W",
      "ConfiguratorMessages": {
        "message": [
          {
            "message": "COUPON EXPIRED",
            "MessageType": "W"
          }
        ]
      }
    },
    {
      "line": "4.00",
      "ConfigStatus": "W",
      "ConfiguratorMessages": {
        "message": [
          {
            "message": "COUPON EXPIRED",
            "MessageType": "W"
          }
        ]
      }
    },
    {
      "line": "5.00",
      "ConfigStatus": "W",
      "ConfiguratorMessages": {
        "message": [
          {
            "message": "COUPON EXPIRED",
            "MessageType": "W"
          },
          {
            "message": "COUPON RESTRICTED FOR ANOTHER PRODUCT",
            "MessageType": "W"
          }
        ]
      }
    }
  ]
}

Below is the apex to deserialize the above JSON but I don't know how to check when the data type of message changes from {} to [].

public class ConfigMessages {

    public List<LineStatus> LineStatus;

    public class LineStatus {
        public String line;
        public String ConfigStatus;
        public ConfiguratorMessages ConfiguratorMessages;
    }

    public class ConfiguratorMessages {
        public Message message;
    }

    public class Message {
        public String MessageType;
        public String message;
    }

    public static ConfigMessages parse(String json) {
        return (ConfigMessages) System.JSON.deserialize(json, ConfigMessages.class);
    }
}

//I have further tried to go the deserialize/serialize/deserialize again route as below but I have no clue how to take this further.

Map<String, Object> responseMap = (Map<String, Object>) JSON.deserializeUntyped(JsonString);   
List<Object> lineStatusObjectList = (List<Object>) responseMap.get('LineStatus');
List<Map<String, Object>> lineStatusResponseMapList = new List<Map<String, Object>>();

for (Object obj : lineStatusObjectList) {
    lineStatusResponseMapList.add((Map<String, Object>)obj);
}//for

String serializedLineStatusJson = JSON.serializePretty(lineStatusResponseMapList);
 for( Map<String, Object> mapObj : lineStatusResponseMapList ){
     for (String attributeName : mapObj.keyset()) {
         Map<String, Object> configuratorMessagesResponseMap =(Map<String, Object>) mapObj.get('ConfiguratorMessages'); 
         System.debug('** configuratorMessagesResponseMap ' + configuratorMessagesResponseMap);
      }          
}   

Best Answer

Pretty ugly, but less work than blank's approach:

Object o = JSON.deserializeUntyped(JsonString);
try {
    // See if an object
    Map<String, Object> m = (Map<String, Object>) o;
    ...
} catch(TypeException e) {
    // Not an object, must be an array
    List<Object> a = (List<Object>) o;
    ...
}