Jackson - Read JSON File



In this example, we will see how to read an external JSON file using the Jackson library.

Dependencies

Let’s first add the following dependencies to the pom.xml:
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.8</version>
</dependency>
This dependency will also transitively add the following libraries to the classpath:
  • jackson-annotations-2.9.8.jar
  • jackson-core-2.9.8.jar
  • jackson-databind-2.9.8.jar
Always use the latest versions on the Maven central repository for Jackson databind.

Using JsonParser for Reading JSON Content

In this example, we will read JSON content from external file "post.json" (In a previuos example, we have written JSON content to this file).
Let's first create the JsonParser using JsonFactory.createJsonParser() method and use it's nextToken() methods to read each JSON string as token. Check each token and process accordingly. Here is a complete program to read JSON content from an external file:
package net.javaguides.jackson;

import java.io.File;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;

/**
 * Read JSON from file using JsonParser
 * @author Ramesh Fadatare
 *
 */
public class ReadJsonUsingJsonParser {

    public static void main(String[] args) throws IOException {
        JsonFactory factory = new JsonFactory();

        // Create Reader/InputStream/File
        File file = new File("post.json");

        // Create JsonParser
        JsonParser parser = factory.createParser(file);

        JsonToken token = parser.nextToken(); // Read first object i.e. {

        // Read JSON object
        token = parser.nextToken();
        if (token == JsonToken.FIELD_NAME && "id".equals(parser.getCurrentName())) {
            token = parser.nextToken();
            if (token == JsonToken.VALUE_STRING) {
                System.out.println("ID : " + parser.getText());
            }
        }
        token = parser.nextToken();
        if (token == JsonToken.FIELD_NAME && "title".equals(parser.getCurrentName())) {
            token = parser.nextToken();
            if (token == JsonToken.VALUE_STRING) {
                System.out.println("title : " + parser.getText());
            }
        }
        token = parser.nextToken();
        if (token == JsonToken.FIELD_NAME && "description".equals(parser.getCurrentName())) {
            token = parser.nextToken();
            if (token == JsonToken.VALUE_STRING) {
                System.out.println("description : " + parser.getText());
            }
        }

        token = parser.nextToken();
        if (token == JsonToken.FIELD_NAME && "content".equals(parser.getCurrentName())) {
            token = parser.nextToken();
            if (token == JsonToken.VALUE_STRING) {
                System.out.println("content : " + parser.getText());
            }
        }

        // Reading JSON Array
        token = parser.nextToken();
        if (token == JsonToken.FIELD_NAME && "tags".equals(parser.getCurrentName())) {

            System.out.println("Post tags are - ");
            token = parser.nextToken(); // // Read left bracket i.e. [
            // Loop to print array elements until right bracket i.e ]
            while (token != JsonToken.END_ARRAY) {
                token = parser.nextToken();

                if (token == JsonToken.VALUE_STRING) {
                    System.out.println(parser.getText());
                }
            }
            System.out.println();
        }
        parser.close();
    }
}
Output:
title : Jackson JSON API Guide
description : Post about Jackson JSON API
content : HTML content here
Post tags are - 
JSON
Java
Jackson

Comments