Mapping of JSON attributes to integers using Spring Java

Lily :

I have a lot of JSON files in the following format. I want to map one attribute named Timings to integer.

test.json

"Rating": {
  "ratingValue": "4.636365",
  "bestRating": "5",
  "worstRating": "1",
  "ratingCount": "66"
 },
 "Timings": {
  "cookTime": "PT1H",
  "prepTime": "PT10M",
  "totalTime": "PT1H10M"
 }

I want to store the output in another JSON file after mapping. Let say, totalTime in Timings is 1H10M then we assign this as "totalTime:7". If its, "30M" we can assign this as "totalTime:3". I want to do this using java.

Required output

"Rating": 
{
  "ratingValue": "4.636365",
},
 "Timings": 
{
    "totalTime": "7"
}
Anish B. :

I tried this :

class Timings {

    private String cookTime;
    private String prepTime;
    private String totalTime;

    public String getCookTime() {
        return cookTime;
    }

    public void setCookTime(String cookTime) {
        this.cookTime = cookTime;
    }

    public String getPrepTime() {
        return prepTime;
    }

    public void setPrepTime(String prepTime) {
        this.prepTime = prepTime;
    }

    public String getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(String totalTime) {
        this.totalTime = totalTime;
    }

    @Override
    public String toString() {
        return "Timings [cookTime=" + cookTime + ", prepTime=" + prepTime + ", totalTime=" + totalTime + "]";
    }

}
public class Test {
    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        Timings obj = mapper.readValue(new File("C:\\Users\\Anish\\Desktop\\abc.json"), Timings.class);
        String totalTime = obj.getTotalTime().split("PT")[1];
        int total = 0;
        if (totalTime != null && !totalTime.isEmpty()) {
            total = returnTotalTime(totalTime);
        }
        ObjectNode mainNode = mapper.createObjectNode();
        ObjectNode timingNode = mapper.createObjectNode();
        childNode.put("totalTime", (total / 10));
        mainNode.set("Timings", timingNode);
        String json = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rootNode);
        System.out.println(json);

    }

    private static int returnTotalTime(String totalTime) {
        if (!totalTime.split("H")[0].isEmpty()) {
            if (totalTime.split("H")[0].contains("M")) {
                return totalTime.split("H")[0].split("M")[0].isEmpty() ? 0
                        : Integer.parseInt(totalTime.split("H")[0].split("M")[0]);
            }
            return totalTime.split("H")[1].split("M")[0].isEmpty() ? 0
                    : (Integer.parseInt(totalTime.split("H")[0]) * 60)
                            + Integer.parseInt(totalTime.split("H")[1].split("M")[0]);
        }
        return 0;
    }
}

abc.json

{
  "cookTime": "PT1H",
  "prepTime": "PT10M",
  "totalTime": "PT1H10M"
}

Output :

{
  "Timings" : {
    "totalTime" : "7"
  }
}

When "totalTime": "PT30M", then :

Output :

{
  "Timings" : {
    "totalTime" : "3"
  }
}

When "totalTime": "PT23M", then :

Output :

{
  "Timings" : {
    "totalTime" : "2"
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=345734&siteId=1