Convert Json from One Format to Another Json Format in Java

Convert Json from One Format to Another Json Format in Java

I have to covert a specific JSON response and covert to some other format

For E.g. My input JSON is like

JSON1

[
    {
        "propertyId":10081,
        "roomId":0,
        "startDate":"2018-01-29T00:00:00",
        "endDate":"2018-01-30T00:00:00",
        "priority":1,
        "message":"Test it",
        "id":158,
        "dateModifiedUtc":null
    },
    {
        "propertyId":10081,
        "roomId":10021855,
        "startDate":"2018-01-29T00:00:00",
        "endDate":"2018-01-30T00:00:00",
        "priority":2,
        "message":"Check how it works",
        "id":159,
        "dateModifiedUtc":null
    },
    {
        "propertyId":10081,
        "roomId":10021855,
        "startDate":"2018-01-29T00:00:00",
        "endDate":"2018-01-30T00:00:00",
        "priority":2,
        "message":"Check how it works",
        "id":160,
        "dateModifiedUtc":null
    }
]

I need to replace some values for roomId and then convert json to below format and send it to some other endpoint

JSON2

{
    "sitenotification":[
        {
            "roomid":"10601",
            "priority":1,
            "startdate":"2017-08-10T15:50:52+03:00",
            "enddate":"2017-08-15T15:50:52+03:00",
            "sitemessage":"test"
        },
        {
            "roomid":"10601",
            "priority":1,
            "startdate":"2017-08-10T15:50:52+03:00",
            "enddate":"2017-08-15T15:50:52+03:00",
            "customermessage":"test 2"
        }
    ]
}

I tried doing this in couple of ways like Deserialization using jackson and mapping it to specific class like below

@JsonIgnoreProperties(ignoreUnknown = true)
public class FatNotificationsAttributesDataModel {

    @JsonProperty("propertyId")
    public int propertyId;
    @JsonProperty("roomId")
    public int roomId;
    @JsonProperty("startDate")
    public String startDate;
    @JsonProperty("endDate")
    public String endDate;
    @JsonProperty("priority")
    public int priority;
    @JsonProperty("message")
    public String message;
    @JsonProperty("id")
    public int id;
    @JsonProperty("dateModifiedUtc")
    public String dateModifiedUtc;

}

and then again serialising it using a different class

@JsonSerialize(using = CustomSerializer.class)
public class FinalDataModel {

    public int roomId;

    public String startDate;

    public String endDate;

    public int priority;

    public String message;

}

But somehow the conversion is not working. Do we have any better way to achieve this?

6

2 Answers

Use Jolt Convertor. Simplest, and esiest way to convert any JSON to any other JSON format.

You can achieve that by using JSONObject and JSONArray

//Create a JSON array with your json string
JSONArray array = new JSONArray(jsonString);
JSONArray newArray = new JSONArray();
//loop on your JSONArray elements
for (i=0, i<array.size(),i++) {
    //Get each separate object into a JSONObject
    JSONObject obj = array.getJSONObject(i);
    //let say you want to modify id for example
    obj.put("id", 12345);
    //push the modifyed object into your new array
    newArray.put(obj);
}
//transform the array into a string
String json = newArray.toString();
4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.