Skip to content
Advertisement

Mapping nested JSON values in a Java class

I can’t figure out how to handle nested JSON values in my Java classes. To make it simple as possible, I created four Java classes with every ‘nested level’. However, I’m trying to have all these values in one Java class. How can I do this?

Json:

{
   "_embedded":{
      "events":[
         {
            "name":"KISS | End Of The Road World Tour",
            "dates":{
               "start":{
                  "dateTime":"2021-06-12T19:00:00Z"
               },
               "classifications":[
                  {
                     "name":"Rock"
                  }
               ],
               "_embedded":{
                  "venues":[
                     {
                        "name":"Atlas Arena"
                     }
                  ]
               }
            }
         }
      ]
   }
}

Java classes:

@Data
public class EventList {

    @JsonProperty("_embedded")
    private Events events;
}


@Data
public class Events {

    @JsonProperty("events")
    public List<EventDetails> eventsList;
}

@Data
public class EventDetails {

    private String name;
    @JsonProperty("dates.start.dateTime")
    private String startDate;
    @JsonProperty("classifications.genre.name")
    private String musicType;
    @JsonProperty("_embedded.venues")
    private List<Venues> eventPlaceName;
}

@Data
public class Venues {

    private String name;
}

Advertisement

Answer

You can club all the supporting classes in one class like below :

@Data
public class Start {
    @JsonProperty("dateTime")
    public Date dateTime;

    public static class Venue {
        @JsonProperty("name")
        public String name;
    }

    @Data
    public static class Classification {
        @JsonProperty("name")
        public String name;
    }

    @Data
    public static class Embedded2 {
        @JsonProperty("venues")
        public List<Venue> venues;
    }

    @Data
    public static class Dates {
        @JsonProperty("start")
        public Start start;
        @JsonProperty("classifications")
        public List<Classification> classifications;
        @JsonProperty("_embedded")
        public Embedded2 _embedded;
    }

    @Data
    public static class Event {
        @JsonProperty("name")
        public String name;
        @JsonProperty("dates")
        public Dates dates;
    }

    @Data
    public static class Embedded {
        @JsonProperty("events")
        public List<Event> events;
    }

    @Data
    public static class Root {
        @JsonProperty("_embedded")
        public Embedded _embedded;
    }
}

You can test (I am using Jackson for deserialization)
create ObjectMapper class and deserialize into a Root class

public class TestJson {
    public static void main(String[] args) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            Start.Root root = objectMapper.readValue(new File("C:\Anurag\Development\CodeBase\demo\src\main\java\com\example\demo\domain\testJson\test.json"), Start.Root.class);
            System.out.println(root);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}

-When debugging, you’ll notice that our objects have been filled accordingly: enter image description here

**Changes done as per your requiement **

@Data
public class EventList {

    @Getter
    @JsonProperty("_embedded")
    private Events events;




    @Data
    public static class Venue {
        @JsonProperty("name")
        public String name;
    }

    @Data
    public static class Classification {
        @JsonProperty("name")
        public String name;
    }

    @Data
    public static class Embedded2 {
        @JsonProperty("venues")
        public List<Venue> venues;
    }

    @Data
    public static class Dates {
        @JsonProperty("start")
        public Start start;
        @JsonProperty("classifications")
        public List<Classification> classifications;
        @JsonProperty("_embedded")
        public Embedded2 _embedded;
    }

    @Data
    public static class EventDetails {
        @JsonProperty("name")
        public String name;
        @JsonProperty("dates")
        public Dates dates;
    }

    @Data
    public static class Events {
        @JsonProperty("events")
        public List<EventDetails> eventsList;
    }

    @Data
    public static class Start {
        @JsonProperty("dateTime")
        public Date dateTime;
    }


}

public class TestJson {
    public static void main(String[] args) {
        List<EventList.EventDetails> anyCity = findEventByCity("any city");
        anyCity.stream().forEach(p-> {
            System.out.println(p);
        });

    }

    @SneakyThrows
    static List<EventList.EventDetails> findEventByCity(String city) {
        ObjectMapper objectMapper = new ObjectMapper();
        EventList eventList = objectMapper.readValue(new File("C:\Anurag\Development\CodeBase\demo\src\main\java\com\example\demo\domain\testJson\test.json"), EventList.class);
        List<EventList.EventDetails> eventsList = eventList.getEvents().getEventsList();
        return eventsList;
    }

    }

ebugging, you'll notice that our objects have been filled accordingly:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement