6

Goal

How can I exclude all empty json objects {}, arrays [] or arrays of empty objects [{},{}] from the JSON response produced by Jackson in a RestController ?

Requirements and context

I'm building a Spring Boot REST API for a client. The API sends requests to a database and must produce a JSON response.

Constraints:

  • The DAO layer sends native SQL queries and receives huge DB results as a List. Developers have to manually map this result to Java objects using indexes (see code below)
  • SQL queries return a lot of null values (these queries can NOT be modified). Because of these null values, Java objects having only null-valued fields are frequently instantiated

Requirements:

  • All fields having null value must be excluded from JSON responses. Already implemented using Jackson @JsonInclude(JsonInclude.Include.NON_NULL) annotation
  • All empty json objects {} or arrays [], or arrays of empty objects [{},{}] must be excluded from the JSON response. This is where I'm stuck (Please see example below)

Code

Manual mapping in DAO layer:

public List<A> daoMethod() {
    List<Object[]> dbResult = getDbResults();
    List<A> javaObjects = new ArrayList<>();

    // build nested Java objects
    for (Object[] line in dbResult) {
        A a = new A();
        a.setProp1(line[0]);
        a.setProp2(line[1]);
        // and so on...
        javaObjects.add(a);
        return javaObjects ;
    }
}

Controller method:

public ResponseEntity<A> controllerMethod() {
    List<A> javaObjects = myDao.daoMethod();
    return new ResponseEntity(javaObjects, HttpStatus.OK);
}

All DTO classes which must be serialized in the JSON response extend the BaseDto class:

@JsonInclude(JsonInclude.Include.NON_NULL) // removes all fields having NULL value
public abstract class BaseDto implements Serializable{
    // some properties...
}

Actual and expected results

Current JSON output:

{
  prop1: "some string",
  prop2: [{},{},{}],
  prop3: [],
  prop4: {},
}

Expected:

{
  prop1: "some string"
}
3
  • please refer the following link stackoverflow.com/questions/16089651/… Commented Jun 21, 2019 at 3:50
  • Did you find any solution? I'm having the same problem. I also tried JsonInclude.NON_EMPTY,JsonInclude.NON_NULL but without any luck Commented Jan 31, 2020 at 21:24
  • Any one got solution for this ?? Commented Sep 27, 2021 at 10:38

2 Answers 2

1

Try to use NON_EMPTY

@JsonInclude(JsonInclude.Include.NON_EMPTY) 

Value that indicates that only properties with null value, or what is considered empty, are not to be included.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I tried using this annotation with different combinations (class level, type level...) but it doesn't work
0

NON_EMPTY can be used in application.properties to exclude empty objects (including null objects) in response.

spring.jackson.default-property-inclusion=NON_EMPTY

By the way NON_NULL is also applicable to remove null objects only in response.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.