Reading and Writing JSON with JSON Processing API

As a big fan of Java EE 7, recently I had a look at the JSON Processing API.

As you know JSON is a data exchange format widely used in web services and other connected applications. The new JSON Processing API (JSR 353), is a lightweight API to parse, transform, and query JSON data using a object model or streaming model.

You can easily build an JSON Object:

JsonObject model = Json.createObjectBuilder()
 .add("title", "Building Microservices")
 .add("author", "Sam Newman")
 .add("publisher", "O'Reilly")
 .add("pages", 278)
 .add("ISBN", "978-1491950357")
 .add("price", 12.0)
 .add("currency", "EUR")

And writing an JSON model to a stream are just a few lines.

 StringWriter stringWriter = new StringWriter();
 writeModel(stringWriter, model);

 try (JsonWriter jsonWriter = Json.createWriter(stringWriter)) {
 } catch(JsonException je)

 String jsonData = stringWriter.toString();

An JSON file you can read like this:

 InputStream is = getClass().getResourceAsStream(fileName);
 try(JsonReader reader = Json.createReader(is))
     JsonStructure jsonStructure =;
 } catch(JsonException je)
 } finally {

And there is even a streaming model for parsing and writing JSON. Here you can see the parsing.

InputStream is = ...
try(JsonParser parser = Json.createParser(is))
    while (parser.hasNext()) {
        JsonParser.Event event =;
        switch(event) {
            case START_ARRAY:
            case END_ARRAY:
            case START_OBJECT:
            case END_OBJECT:
            case VALUE_FALSE:
            case VALUE_NULL:
            case VALUE_TRUE:
            case KEY_NAME:
                System.out.print(event.toString() + " " +
                parser.getString() + " - ");
            case VALUE_STRING:
            case VALUE_NUMBER:
                System.out.println(event.toString() + " " +
} catch(JsonException je)

And here comes the writing example.

FileWriter writer = new FileWriter(fileName);

try(JsonGenerator generator = Json.createGenerator(writer))
        .write("title", "Building Microservices")
        .write("author", "Sam Newman")
        .write("publisher", "O'Reilly")
        .write("pages", 278)
        .write("ISBN", "978-1491950357")
        .write("price", 12.0)
        .write("currency", "EUR")

It was fun playing around with the API. I think the fact that JSON Processing (Project) does not explicitly support JSON binding limits the usage. I read that a future JSR may consider a JSON Binding similar to JAXB. So you can use the Java API for JSON Processing with JAX-RS resource methods, but when does it make sense?

In my opinion, if you are not the owner of the bounded context and model then you can use the JSON Processing API. Otherwise I would use JAXB with annotations. Jersey, the reference implementation for JAX-RS supports for binding JSON data from Restful resource methods to Java objects using JAXB. With JAXB you can automatically read and write XML and JSON.

I hope you had fun reading my article.

GitHub-Mark-32px Examples on Github:

Here you can get an overview:
This is the project site:
and here you can have a look a API Javadoc: