jackson from json to object code example

Example 1: convert json to object jackson

ObjectMapper objectMapper = new ObjectMapper();

String carJson =
    "{ \"brand\" : \"Mercedes\", \"doors\" : 5 }";

try {
    Car car = objectMapper.readValue(carJson, Car.class);

    System.out.println("car brand = " + car.getBrand());
    System.out.println("car doors = " + car.getDoors());
} catch (IOException e) {
    e.printStackTrace();
}

Example 2: jackson object to json

val objectMapper = ObjectMapper()
val car = Car("yellow", "renault")
objectMapper.writeValueAsString(car)