0

My application is consuming a third party API which produces a JSON response. I have mapped relevant pojo classes to the respective JSON responses. But I have a difficulty with mapping a particular response which has a property "message" that is sometimes String and sometimes Object.

The datatype of the property "message" changes for particular requests for the same API call. I don't want to parse the JSON string using JSONObject or a JSONArray instead I want to map this response to a pojo class.

Can anybody please help me with this? Thanks!

Response 1

{
    "Message": {
        "name": "John Doe"
    }
}

Response 2

{
    "Message": "some error message"
}

I asked the API provider to change this behaviour but he/she refuses to my request saying he does not have control over what is produced in the json response.

Rakesh
  • 4,264
  • 4
  • 32
  • 58
  • Use Map or for getting response and now you will get response object as Map then use condition statement and based on tht condition convert it into pojo by using ObjectMapper – Kalaiselvan Jun 14 '17 at 04:54
  • 1
    I think this should solve your problem https://stackoverflow.com/questions/13891415/deserializing-json-with-multiple-types-in-one-field – Shafin Mahmud Jun 14 '17 at 05:59

0 Answers0