0

I need to modify an existing old code that uses javax.json.JsonObject and javax.json.JsonArray library to handle JSON. Since these are interfaces so I cannot create a new object and add the required data inside for loop as I have done in the past with the help of JSON.simple libraries. I need to create a JsonArray that contains different JsonObjects but the final output contains only the last JsonObject. Here is the code:

import javax.json.JsonArray;
import javax.json.JsonObject;
class Test {
    public void myMethod() {
        Map<String, String> ep = null;
        JsonArray response = null;
        JsonObject epObj = null;
        JsonObject epDetails = null;
        try {
            ep = getRequiredEp();
            if (ep != null && !ep.isEmpty()) {
                for (String uuid : ep.keySet()) {
                    ERU router = ERU.findByNFuuid(em, uuid);
                    if (router != null) {
                        ERD unit = router.findCurrent(em);
                        epDetails = Json.createObjectBuilder()
                                .add("name", ep.get(uuid))
                                .add("devId", unit.unit.getId())
                                .add("clientId", unit.store.id).build();
                        epObj = Json.createObjectBuilder()
                                .add(uuid, epDetails).build();
                        response = Json.createArrayBuilder()
                                .add(epObj).build();
                    }
                }
                logger.info("FINAL RESPONSE = {}", response);
            }
        } catch (Exception e) {
            logger.info(e.getMessage(), e);
        }
    }
}

My JsonObject contains two objects. But when I print Final response it give only the last object. Please suggest how to add both the objects in the JsonArray.

0

1 Answer 1

1

That's because you are assigning a new JsonArray to the response on each iteration. I think you should use the single array builder inside your loop and build the array outside, like that:

public void myMethod() {
    Map<String, String> ep = null;
    var builder = Json.createArrayBuilder();
    JsonArray response = null;
    JsonObject epObj = null;
    JsonObject epDetails = null;
    try {
        ep = getRequiredEp();
        if (ep != null && !ep.isEmpty()) {
            for (String uuid : ep.keySet()) {
                ERU router = ERU.findByNFuuid(em, uuid);
                if (router != null) {
                    ERD unit = router.findCurrent(em);
                    epDetails = Json.createObjectBuilder()
                            .add("name", ep.get(uuid))
                            .add("devId", unit.unit.getId())
                            .add("clientId", unit.store.id).build();
                    epObj = Json.createObjectBuilder()
                            .add(uuid, epDetails).build();
                    builder.add(epObj);
                }
            }
            response = builder.build();
            logger.info("FINAL RESPONSE = {}", response);
        }
    } catch (Exception e) {
        logger.info(e.getMessage(), e);
    }
}
Sign up to request clarification or add additional context in comments.

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.