0

I tried looking for similar questions, and found this one Use Jackson to parse and unnamed array

However, I have a problem. My JSON has this unnamed array on a Nth level - very simplified version:

{
   "section":{
      "ID":"",
      "code":{
         "code":"",
         "codeSystem":"",
         "codeSystemName":""
      },
      "entry":[
         {
            "act":{
               "classCode":"",
               "entryRelationship":{
                  "name":""
               }
            },
            "id":""
         },
         {
            "act":{
               "classCode":"",
               "entryRelationship":{
                  "name":""
               }
            },
            "id":""
         }
      ]
   }
}

There are even more levels higher than section, but this should represent the main problem. I have this "entry" key that can have multiple "act" and "id" (as properties of an unnamed object). I'm already using the ObjectMapper and @JsonProperty tools from the jackson library, and I want to keep things clean. A piece of code from the section (again, very simplified):

public class ComponentSection {

    @JsonProperty("ID")
    private String id;
    @JsonProperty("code")
    private Code code;
    @JsonProperty("entry")
    private SectionEntry entry;
}

Is there anything I can do to map this JSON?

Thanks!

Mauricio
  • 186
  • 1
  • 11

2 Answers2

2

change

@JsonProperty("entry")
private SectionEntry entry;

to

@JsonProperty("entry")
private List<SectionEntry> entry;
1

The value of "entry" is a list

you need to make class like this:

import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;

public class ComponentSection {

    @JsonProperty("section")
    private Section section;

    static class Section {
        @JsonProperty("ID")
        private String id;
        @JsonProperty("code")
        private Code code;
        @JsonProperty("entry")
        private List<Entry> entry;
    }

    static class Code {
        @JsonProperty("code")
        private String code;
        @JsonProperty("codeSystem")
        private String codeSystem;
        @JsonProperty("codeSystemName")
        private String codeSystemName;
    }

    static class Entry {
        @JsonProperty("act")
        private Act act;
        @JsonProperty("id")
        private String id;
    }

    static class Act {
        @JsonProperty("classCode")
        private String classCode;
        @JsonProperty("entryRelationship")
        private EntryRelationship entryRelationship;
    }

    static class EntryRelationship {
        @JsonProperty("name")
        private String name;
    }

}

You can also separate the classes to have a better order.

Example of use:

    public static void main(String[] args) {
        ComponentSection componentSection = null;
        try {
            componentSection = new ObjectMapper().readValue(jsonString, ComponentSection.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(componentSection);
    }
fneira
  • 291
  • 1
  • 8