When I add a list of test JsonObjects, only the last JsonObject is add to the JsonArray. I do not understand why because I am following documentation from oracle. https://docs.oracle.com/javaee/7/api/javax/json/JsonArray.html
Methods from class called TestRun
public JsonObject convertToJSONObject() {
return Json.createObjectBuilder()
.add("name", name)
.add("value", value)
.add("timestamp", convertZonedDateTime())
.build();
}
public JsonArray convertToJSONArray(JsonObject object) {
return Json.createArrayBuilder()
.add(object)
.build();
}
Main (Note: I am using faker to generate random info)
TestRun testRun;
Faker faker = new Faker(); //Faker to generate random level_name
JsonArray jsonArray = null;
for (int i = 0; i < 2; i++) {
testRun = new TestRun(faker.name().firstName(), faker.number().numberBetween(1, 200));
JsonObject object = testRun.convertToJSONObject();
jsonArray = testRun.convertToJSONArray(object);
}
System.out.println(jsonArray);
convertToJSONArrayis clearly creating a new array before adding the element, so it's normal that you always have the last element of the loop. I think you rather want to take aJsonArrayin input and add the element to this.