Posted In: Jackson 2X, JSON

How to use Jackson 2.X to read big JSON file

Usecase – You have JSON data stored at a location. It is big so do not have luxury to read it fully in ObjectMapper or you just need use some part of JSON. Jackson API provides streaming JSON parser class to do that.

Maven Jackson 2X

<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-core</artifactId>
	<version>2.8.6</version>
</dependency>

Classes used from Jackson 2X

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

Test data

[
  {
    "albumId": 1,
    "id": 1,
    "title": "accusamus beatae ad facilis cum similique qui sunt",
    "url": "http://placehold.it/600/92c952",
    "thumbnailUrl": "http://placehold.it/150/92c952"
  },
  {
    "albumId": 1,
    "id": 2,
    "title": "reprehenderit est deserunt velit ipsam",
    "url": "http://placehold.it/600/771796",
    "thumbnailUrl": "http://placehold.it/150/771796"
  }
]
package com.example.testing.json;

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

import org.apache.commons.lang3.StringUtils;

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

public class JacksonReadJson {

	public static void main(String[] args) {
		new JacksonReadJson().proceed();
	}

	public void proceed() {
		try {
			JsonFactory jfactory = new JsonFactory();
			JsonParser jParser = jfactory.createParser(new File("e:\\abhijit\\javausecase\\bigjson.json"));
			//END_ARRAY is returned when encountering ']' which signals ending of an Array value
			while (jParser.nextToken() != JsonToken.END_ARRAY) {
				if(StringUtils.equals("url", jParser.getText())) {
					jParser.nextToken();
					System.out.println(jParser.getText());
				}
			}
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Output

http://placehold.it/600/92c952
http://placehold.it/600/771796

Tags: Tags:

by , on August 8th, 2017