user2868900
user2868900

Reputation: 771

Keep null values – Java POJO to org.JSONObject

I am creating a RESTful API service in Java using Spring Boot. I have a class from which I am trying to create a JSON Object. Everything is working great - except the json object from the class instance is omitting the null values. I would like to keep the keys and have the value set to null, rather than stripping them completely from the output.

The output is as follows (shortened for brevity)

    "questions": [
        {
            "question": "question1",
            "svg": "svg1",
            "questionNum": 1
        },
        {
            "question": "question2"
            "svg": "q2",
            "questionNum": 2
        }
    ],

Preferred output

    "questions": [
        {
            "question": "question1",
            "svg": "svg1",
            "questionNum": 1, 
            "shuffledNum": null, 
            "answer": null

        },
        {
            "question": "question2"
            "svg": "q2",
            "questionNum": 2,
            "shuffledNum": null,
            "answer": null 
        }
    ],

Here is the JsonQuestionObject:

public class JsonQuestionObject {

    private Integer questionNum;
    private String svg;
    private Integer shuffledNum = null;
    private String question;
    private Integer answer = null;

    //Getters & Setters
}

Here is the JSON Creator method (which takes an array of JsonQuestionObjects)

public JSONObject createQuestionsJson(ArrayList<JsonQuestionObject> questions) {
    JSONObject output = new JSONObject();
    JSONArray content = new JSONArray();
    JSONObject subContent = new JSONObject();
    JSONArray qarray = new JSONArray();


    for (int i = 0; i < questions.size(); i++) {
        JsonQuestionObject q = questions.get(i);
        Integer qnum = q.getQuestionNum();
        JSONObject qitem = new JSONObject(q);

        if (qnum < 5) {
            qarray.put(qitem);
        } else if (qnum > 4 && qnum < 7) {
            qarray.put(qitem);
        } else if (qnum > 6 && qnum < 9 ) {
            qarray.put(qitem);
        } else if (qnum > 8 &&) {
            qarray.put(qitem);
        }
    }
    subContent.put("questions", qarray);
    content.put(subContent);
    output.put("output", content);

    return output;
}

Is there any way of keeping these null values other than creating the JSONObject manually by using the setters & getters of each property and setting JSONObject.put("answer", JSONObject.NULL) ?

Much appreciated for any help.

Upvotes: 2

Views: 5264

Answers (2)

Michał Ziober
Michał Ziober

Reputation: 38665

When you create new JSONObject with constructor public JSONObject(Object bean) all null values are skipped by internal implementation. To keep null-s you have to create and populate new JSONObject by yourself.

From other side, you do not need to use org.json.* library. You can construct similar structure using Maps and Lists. See below structure:

JsonQuestionObject[] questionArray = {new JsonQuestionObject()};
HashMap<String, Object> questions = new HashMap<>();
questions.put("questions", questionArray);

List<Object> questionsList = new ArrayList<>();
questionsList.add(questions);

Map<String, Object> result = Collections.singletonMap("output", questionsList);

You can return Map in your method and it will be automatically serialised to JSON. You can enable nulls using Spring Boot properties or add JsonInclude annotation:

@JsonInclude(JsonInclude.Include.ALWAYS)
public class JsonQuestionObject

Upvotes: 2

Bor Laze
Bor Laze

Reputation: 2516

Try to add spring.jackson.default-property-inclusion=always to your application.properties.

Also, take a look on other settings from spring.jackson family for other settings of JSON serialization.

Upvotes: 0

Related Questions