skip to Main Content

Is there a way in Jackson to map json array’s positions to different class properties?

For example if we have an api that returns person properties as array (0 is the id, 1 is the name,2 is the age, etc.) how to map this to an object that has properties id, name and age?

"person": [
1,
"Evgeni",
35
]


record Person(Long id, String name, Integer age)

Looking for something like:

@JsonProperty("<get the item at position 1>") String name

2

Answers


  1. You can create a custom deserializer using the JsonDeserializer interface and map the contents of a JSON array to the corresponding properties by overriding the class deserialize(JsonParser p, DeserializationContext ctxt).

    Create a custom deserializer:

    public class FooBarDeserializer extends JsonDeserializer<FooBar> {
    @Override
    public FooBar deserialize(JsonParser p, DeserializationContext ctxt) 
            throws IOException, JsonProcessingException {
        JsonNode node = p.getCodec().readTree(p);
        int foo = node.get(0).asInt();
        String bar = node.get(1).asText();
    
        return new FooBar(foo, bar);
      }
    }
    

    Annotate the class with the custom deserializer:

    @JsonDeserialize(using = FooBarDeserializer.class)
    public class FooBar {
      private int foo;
      private String bar;
    }
    

    Deserialize the JSON:

    public static void main(String[] args) throws Exception {
      String json = "[42, "hello"]";
    
      ObjectMapper mapper = new ObjectMapper();
      FooBar fooBar = mapper.readValue(json, FooBar.class);
    
      ...
    }
    
    Login or Signup to reply.
  2. You may try to use JSON library Josson to transform the JSON array into object.

    https://github.com/octomix/josson

    POJO Person

    public static class Person {
        @JsonProperty("0")
        private Long id;
    
        @JsonProperty("1")
        private String name;
    
        @JsonProperty("2")
        private Integer age;
    
        @Override
        public String toString() {
            return "Person{" +
                    "id=" + id +
                    ", name='" + name + ''' +
                    ", age=" + age +
                    '}';
        }
    }
    

    Deserialize, Transform and Convert

    Josson josson = Josson.fromJsonString("{"person":[1,"Evgeni",35]}");
    
    JsonNode node = josson.getNode("person.map(#::?).mergeObjects()");
    System.out.println("nJsonNode:n" + node.toPrettyString());
    
    Person person = Josson.readValueFor(node, Person.class);
    System.out.println("nPerson:n" + person);
    

    Transformation expression:

    • map() : Construct an object.
      • # denote the zero based array index.
      • :: extract the field value to become the key name.
      • ? denote the current node value.
    • mergeObjects() : Merge multiple objects into one object.

    Output

    JsonNode:
    {
      "0" : 1,
      "1" : "Evgeni",
      "2" : 35
    }
    
    Person:
    Person{id=1, name='Evgeni', age=35}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search