1

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);
1
  • I don't know very well the javax's Json (I use Jackson), but your method convertToJSONArray is 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 a JsonArray in input and add the element to this. Commented Jun 27, 2022 at 16:11

2 Answers 2

1

TestRun.java

import javax.json.Json;
import javax.json.JsonObject;
import java.time.ZonedDateTime;

public class TestRun {
    private String name;
    private Integer value;
    private ZonedDateTime timestamp;

    public TestRun(String name, Integer value, ZonedDateTime timestamp) {
        this.name = name;
        this.value = value;
        this.timestamp = timestamp;
    }

    public JsonObject convertToJSONObject() {
        return Json.createObjectBuilder()
                .add("name", name)
                .add("value", value)
                .add("timestamp", convertZonedDateTime())
                .build();
    }

    private String convertZonedDateTime() {
        return timestamp.toString(); // replace with yours code
    }
}

Class with main method:

import com.github.javafaker.Faker;
import javax.json.Json;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import java.time.ZonedDateTime;

public class FakerExample {

    public static void main(String[] args) {
        Faker faker = new Faker();
        JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();

        for (int i = 0; i < 2; i++) {
            var testRun = new TestRun(faker.name().firstName(), faker.number().numberBetween(1, 200), ZonedDateTime.now());
            JsonObject object = testRun.convertToJSONObject();
            jsonArrayBuilder.add(object);
        }

        System.out.println(jsonArrayBuilder.build());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to create an JsonArrayBuilder inside for loop instead of creating JsonArray at each step. Then build the builder outside of for loop like:

JsonArrayBuilder arrayBuilder = null;
for (int i = 0; i < 2; i++) {
      testRun = new TestRun(faker.name().firstName(), faker.number().numberBetween(1, 200));
      JsonObject jsonObject = testRun.convertToJSONObject();
      if (arrayBuilder == null) {
         arrayBuilder = Json.createArrayBuilder().add(jsonObject);
      } else {
         arrayBuilder = arrayBuilder.add(jsonObject);
      }

}

jsonArray = arrayBuilder.build();

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.