1

I've got a JSON that looks like this

{
    "file": "sample.txt",
    "valid": "true",
    "parameters": {
         "size": "15kb",
         "charset": "UTF-8",
         ....
    }
}

But I want to deserialize it as a single object. Not like this

class ValidatedFile {
    String file;
    boolean valid;
    FileParameters params;
}

but like this

class ValidatedFile {
    String file;
    boolean valid;
    String size;
    String charset;
    ....
}

I need to do some kind of unwrapping of this object. How to do it using jackson?

lapots
  • 12,553
  • 32
  • 121
  • 242
  • whatever parameters you have written in FileParameters model class instead of writting there write in ValidatedFile class. – Tejal Dec 25 '18 at 09:07

1 Answers1

3

Use @JsonProperty("parameters"):

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.Map;

public class Product {

    String file;
    boolean valid;
    String size;
    String charset;



    @JsonProperty("parameters")
    private void unpackNested(Map<String,Object> parameters) {
        this.size = (String)parameters.get("size");
        this.charset = (String)parameters.get("charset");
    }

}

Other approaches.

xingbin
  • 27,410
  • 9
  • 53
  • 103