Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

Master Dynamic JSON in Spring Boot 3: 4 Practical Techniques

Master Dynamic JSON in Spring Boot 3: 4 Practical Techniques

This article explains why Spring Boot 3 uses Jackson for JSON handling, describes four effective methods—JsonNode, Map, @JsonAnySetter, and custom deserializer—to process dynamic JSON properties, and promotes a continuously updated collection of over 90 Spring Boot case studies with source code.

Spring Boot 3 uses Jackson for JSON handling, allowing easy serialization and deserialization. When JSON contains dynamic properties not defined in the POJO, several strategies can be applied.

1. Introduction

Jackson handles predefined JSON structures automatically, but dynamic attributes require special techniques.

2. Practical Cases

2.1 Use JsonNode property

Define a JsonNode field in the POJO to capture the dynamic part.

public class Product {
    // other properties
    private JsonNode details;
    // getters, setters
}

Test code:

ObjectMapper objectMapper = new ObjectMapper();
String json = "{ \"name\": \"SpringBoot实战案例100例\", \"category\": \"book\", \"details\": { \"price\": \"70\", \"author\": \"XGPack\" } }";
Product product = objectMapper.readValue(json, Product.class);
System.err.printf("name: %s, category: %s%n", product.getName(), product.getCategory());
System.out.println("--------------------------------");
System.err.printf("price: %s, author: %s%n",
    product.getDetails().get("price").asText(),
    product.getDetails().get("author").asText());

Output:

JsonNode output
JsonNode output

2.2 Use Map collection

Store dynamic attributes in a Map<String,Object> field.

public class Product_Map {
    // other properties
    private Map<String, Object> details;
    // getters, setters
}

Test code:

ObjectMapper objectMapper = ...;
String json = ...;
Product product = objectMapper.readValue(json, Product.class);
System.err.printf("name: %s, category: %s%n", product.getName(), product.getCategory());
System.out.println("--------------------------------");
System.err.printf("price: %s, author: %s%n",
    product.getDetails().get("price"),
    product.getDetails().get("author"));

2.3 Use @JsonAnySetter

Annotate a setter method to capture any unknown properties into a map.

public class Product {
    // other properties
    private Map<String, Object> details = new LinkedHashMap<>();
    @JsonAnySetter
    public void setDetail(String key, Object value) {
        details.put(key, value);
    }
    // getters, setters
}

Test code similar to previous examples; output shown below.

JsonAnySetter output
JsonAnySetter output

2.4 Custom deserializer

Implement a StdDeserializer to manually parse known and dynamic fields.

public class ProductDeserializer extends StdDeserializer<Product> {
    public ProductDeserializer() { this(null); }
    public ProductDeserializer(Class<?> vc) { super(vc); }
    @Override
    public Product deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
        JsonNode node = jp.getCodec().readTree(jp);
        String name = node.get("name").asText();
        String category = node.get("category").asText();
        JsonNode detailsNode = node.get("details");
        String price = detailsNode.get("price").asText();
        String author = detailsNode.get("author").asText();
        Map<String, Object> details = new HashMap<>();
        details.put("price", price);
        details.put("author", author);
        return new Product(name, category, details);
    }
}

Register the deserializer:

ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addDeserializer(Product.class, new ProductDeserializer());
objectMapper.registerModule(module);

Alternatively, annotate the POJO with @JsonDeserialize(using = ProductDeserializer.class) or use @JsonComponent in a Spring Boot application.

The article also announces a Spring Boot 3 case collection containing over 90 practical articles, promising permanent updates and offering source code and detailed MD notes to subscribers.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
JavaSpring BootJacksonCustom DeserializerDynamic JSONJsonAnySetterJsonNode

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion