Search code examples
springspring-bootlombokpojo

Map/access array inside object on Pojo


How can I access and map an array inside an object on Pojo Spring Boot?

This is what I wanted to map

{
    "customers": [
        {
            "customerId": "0434556574",
            "paymentCode": "90501"
        }
    ]
}


Solution

  • You would need to classes to match the JSON structure:

    public class CustomersData {
        private List<Customer> customers;
    
        // Constructor, getter and setter
    }
    
    public class Customer {
        private String customerId;
        private String paymentCode;
    
        // Constructor, getter and setter
    }
    

    Important thing is to make sure your JSON properties name matches the attributes name in your Java classes (thus avoiding explicit mapping between them).