-1

Empty json object {} inside array is mapping to request and list is showing as size 1.

How to validate this kind of json objects.

    {
     "Employee":{
      "name":"Dhruv",
      "code":"emp123",
      "designation":"Accountant",
      "departments":[{}]
      }
    }

Employee DTO:

public class Employee {
  private String name;
  private String code;
  private String designation;
  private List<Department> departments;
}

public class Department{
  private String deptName;
  private string deptCode;
}
Bishan
  • 14,610
  • 50
  • 157
  • 245
Rafeek
  • 1
  • 2
  • Does this answer your question? [How to test if JSON Collection object is empty in Java](https://stackoverflow.com/questions/19170338/how-to-test-if-json-collection-object-is-empty-in-java) – Bishan Feb 25 '22 at 02:54
  • What is the error? – Serge Feb 25 '22 at 03:16

1 Answers1

0

The size will be shown as 1 as there is an empty object within the json (Your json is unaware of the model that created it). The only way to check will be,

const hasDepartments = Employee.departments 
    && Employee.departments.length 
    && Employee.departments[0].deptCode;
naveen
  • 51,042
  • 46
  • 158
  • 241