-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
  • 15,211
  • 52
  • 164
  • 258
Rafeek
  • 1
  • 3
  • 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
  • 53,448
  • 46
  • 161
  • 251