Java EE 8: Convenient JsonArray Collector

Java EE 8 with JSON-P 1.1 introduces a implementation which converts a Collection<JsonObject> into a JsonArray instance:

import java.util.Arrays;
import java.util.List;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonObject;
import static;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;

public class JSONPTest {

    private List<JsonObject> list;
    public void init() {
        this.list = Arrays.asList(next(), next());

    public JsonObject next() {
        return Json.createObjectBuilder().
                add("number", System.currentTimeMillis()).
    public void jsonArrayCollector() {
        JsonArray array = this.list.
        assertThat(array.size(), is(list.size()));

In Java EE 7 you would have to either write a custom collector or use a JsonArrayBuilder for the conversion:

        public void toJsonArrayConversion() {
            JsonArrayBuilder builder = Json.createArrayBuilder();
            JsonArray array =;
            assertThat(array.size(), is(list.size()));
See you at Java EE Workshops at Munich Airport, Terminal 2 and especially at Java EE 8 on Java 9. Is Munich's airport too far? Learn from home:

NEW online workshop: WebStandards Igniter (online)

Airport MUC workshops: Java EE 7: Bootstrap, Effective, Architectures, Web, React and Angular, Testing and Microservices

Podcast: and newsletter:

A book about rethinking Java EE Patterns


Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments