Question: I have a Json string which has a string message field.
String:

```
{  "Type" : "Text",
   "Subject" : "data received",
   "Message" :"{\\"language\\":\\"US\\",\\"data\\":\\"signature\\"}"
}

```

I want to convert it into the following structure:
Notification.java

```
public class Notification {
  String type;
  String subject;
  Message message;
}

```

Message.java

```
public class Message {
  String language;
  String data;
}

```

Is there a way in which I can directly convert the string to a Java object of the above structure? I want to avoid deserializing twice.

Answer: 
You can create a custom `Deserializer` to deserialize the `Message` text into `Message` object and annotate the `Message` class with `@JsonDeserialize`:

```
@JsonDeserialize(using = MessageDeserializer.class)
public class Message {
    String language;
    String data;
}

public class MessageDeserializer extends JsonDeserializer<Message> {

    public MessageDeserializer() {
        super();
    }

    @Override
    public Message deserialize(
            final JsonParser jsonParser, final DeserializationContext deserializationContext) throws
            IOException, JsonProcessingException {
        final String messageText = jsonParser.getText(); 
        // parse messageText into Message object
    }
}

```
The [org.json](http://mvnrepository.com/artifact/org.json/json/20160212) library is easy to use:

```
    //Create Json object to parse string
    // str is input string
    JSONObject obj = new JSONObject(str);

    //Create Message
    Message mess = new Message();
    JSONObject obj2 = new JSONObject(obj.getString("Message"));
    mess.data = obj2.getString("data");
    mess.language = obj2.getString("language");

    //Create Notification 
    Notification noti = new Notification();
    noti.message = mess;
    noti.subject = obj.getString("Subject");
    noti.type = obj.getString("Type");

```