Java and JSON processing

JSON processing in Java. Jackson tips.

Published: Friday, 30 January 2015

Our preferred processor is Jackson.


Use version 2 where possible.

Dynamic Beans

To add dynamic fields into your model, you can use the annotation @JsonAnyGetter on a method that returns a Map. This map will be serialized on the same level as the other fields.

public class Person {
    private String name;
    private Map<String, Object> otherFields = new HashMap<String, Object>();

    public Map<String,Object> any() {
        return otherFields;

    public void set(String name, Object value) {
        otherFields.put(name, value);

    public String getName() {
        return name;

    public void setName(String name) { = name;
Person person = new Person();
person.set("age", 300);

new ObjectMapper().writeValue(System.out, person);

The above will output


See sample/