Parsing Large Json Files Using Jackson Streaming Api Example

In terminal duad of JSON tutorials for Java programmers, nosotros convey learned how to parse JSON using JSON-Simple library, parsing JSON array to Java array using GSon, as well as inwards this tutorial nosotros volition larn how to parse a large JSON file inwards Java using Jackson's Streaming API. Jackson is 1 of the most pop JSON processing framework as well as provides iii principal model to parse as well as procedure JSON information including Streaming API, information binding as well as tree model. Out of these three, Streaming industrial plant at lowest flat as well as tin live used to parse huge JSON answer upto fifty-fifty giga bytes of size. If you lot are familiar amongst XML parsing, as well as hence you lot know that how hard it is to parse huge XML files amongst DOM parser because it fully loads the file inwards retentiveness earlier you lot tin procedure it. In illustration you lot convey depression retentiveness e.g. Android devices you lot can't purpose that to parse XML. Thankfully, XML provides SAX as well as StAX parsers which are streaming based as well as tin live used to procedure huge files without loading them completely inwards memory. Out of these two, StAX is fifty-fifty ameliorate because it allows force based processing where customer pulls information from parser instead of parser pushing data, which is the illustration amongst SAX parser. Jackson's Streaming API is similar to StAX parser. You tin force the information you lot desire as well as ignore what you lot don't want. Though surgical procedure doesn't come upward without cost, using Streaming API is picayune hard as well as hence using other Jackson model which provides withdraw mapping betwixt Java as well as Jackson objects. You convey to handgrip all JSON information past times yourself patch using Streaming API.




Benefits of using Jackson Streaming API

There are several advantages of using Jackson's Streaming API to parse JSON String or convert Java object to JSON, but the most of import 1 is that its real efficient. It has to the lowest degree retentiveness as well as processing overhead as well as extremely useful to parse large JSON responses, for illustration a JSON answer containing thousands of club or listing of books or listing of electronic items downloaded from e-commerce sites similar eBay or Amazon. Talking most other 2 model of Jackson API, information binding model converts JSON to as well as from Java object based either musical note or Java edible bean convention, patch Tree Model provides a mutable in-memory tree representation of a JSON document, similar to DOM parser. In short, Streaming API is most powerful, has less retentiveness as well as CPU overhead but tricky to use, patch information binding is ofttimes most convenient, on the other manus Tree Model is most flexible. BTW, both of this model internally uses streaming API to parse JSON strings earlier converting it into respective models.


Library JARs as well as Dependency

In club to endeavor next example, you lot quest to download as well as add together Jackson streaming API inwards your program's classpath. If you lot are using Maven as well as hence you lot tin add together next dependency inwards your pom.xml file :

<dependency>    <groupId>org.codehaus.jackson</groupId>    <artifactId>jackson-xc</artifactId>    <version>1.9.12</version> </dependency>

or but download as well as  add next JAR inwards CLASSPATH of your Java application.

C:\.m2\repository\org\codehaus\jackson\jackson-xc\1.9.12\jackson-xc-1.9.12.jar C:\.m2\repository\org\codehaus\jackson\jackson-core-asl\1.9.12\jackson-core-asl-1.9.12.jar C:\.m2\repository\org\codehaus\jackson\jackson-mapper-asl\1.9.12\jackson-mapper-asl-1.9.12.jar 

It's ofttimes easier to handle dependency using Maven as well as that's why I strongly advise to switch to Maven if you lot are non using it yet. You tin subsequently upgrade to newer version of Jackson library past times but changing 1 line of piece of employment inwards Maven pom.xml file.


Parsing JSON inwards Java using Jackson Streaming API

 In terminal duad of JSON tutorials for Java programmers Parsing Large JSON Files using Jackson Streaming API Example
This API has 2 principal module, 1 fore reading JSON as well as other for writing JSON as well as inwards this tutorial nosotros volition larn both of them. JsonGenerator is used to write JSON patch JsonParser is used to parse a JSON file. To demonstrate both reading as well as writing of JSON information inwards 1 program, I convey created 2 static methods, createJSON() as well as parseJSON(). As refer suggests showtime method creates a JSON file, which is as well as hence read past times parseJSON() method. You tin run across inwards the code that nosotros are dealing amongst quite depression level, nosotros convey non created whatever Java object to correspond content of JSON, instead nosotros are writing as well as reading String, numbers as well as arrays.

You tin acquire an instance of JsonGenerator from JsonFactory shape past times calling createJsonGenerator() method. You tin besides render the encoding you lot are intended to use, inwards our illustration I convey used "UTF-8" which is a convenient default inwards most cases. You tin purpose diverse write() methods to write contents.  Similarly, for parsing JSON, nosotros quest to exercise an instance of JsonParser, which tin besides live obtained from JsonFactory.  We parse JSON past times calling nextToken() method of JsonParser inwards a patch loop until nosotros accomplish JsonToken.END_OBJECT. Jackson API provides method to acquire refer as well as value of token which you lot tin purpose to position data. Similarly patch parsing JSON array, you lot hold off until you lot acquire JsonToken.END_ARRAY identifier. Since nosotros never charge the whole file inwards memory, this method tin live used to read large JSON files amongst sizes from Mega bytes to Giga bytes fifty-fifty amongst minimal retentiveness surroundings e.g. inwards Android smartphones or Java ME enabled devices.

Here is the sample code illustration to read as well as write JSON using Jackson Streaming API :

import java.io.File; import java.io.IOException;  import org.codehaus.jackson.JsonEncoding; import org.codehaus.jackson.JsonFactory; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonGenerator; import org.codehaus.jackson.JsonParser; import org.codehaus.jackson.JsonToken; import org.codehaus.jackson.map.JsonMappingException;  /** * Java programme to demonstrate how to purpose Jackson Streaming API to read as well as * write JSON Strings efficiently as well as fast. * * @author Javin Paul */ public class JsonJacksonStreamingAPIDemo{      public static void main(String args[]) {          System.out.println("Creating JSON file past times using Jackson Streaming API inwards Java");         createJSON("jacksondemo.json");         System.out.println("done");          System.out.println("Parsing JSON file past times using Jackson Streaming API");         parseJSON("jacksondemo.json");         System.out.println("done");     }      /*      * This method exercise JSON String past times using Jackson Streaming API.      */     public static void createJSON(String path) {         try {             JsonFactory jsonfactory = new JsonFactory();             File jsonDoc = new File(path);             JsonGenerator generator = jsonfactory.createJsonGenerator(jsonDoc, JsonEncoding.UTF8);              generator.writeStartObject();             generator.writeStringField("firstname", "Garrison");             generator.writeStringField("lastname", "Paul");             generator.writeNumberField("phone", 847332223);              generator.writeFieldName("address");              generator.writeStartArray();             generator.writeString("Unit - 232");             generator.writeString("Sofia Streat");             generator.writeString("Mumbai");             generator.writeEndArray();              generator.writeEndObject();              generator.close();              System.out.println("JSON file created successfully");          } catch (JsonGenerationException jge) {             jge.printStackTrace();         } catch (JsonMappingException jme) {             jme.printStackTrace();         } catch (IOException ioex) {             ioex.printStackTrace();         }     }      /*      * This method parse JSON String past times using Jackson Streaming API example.      */     public static void parseJSON(String filename) {         try {             JsonFactory jsonfactory = new JsonFactory();             File origin = new File(filename);              JsonParser parser = jsonfactory.createJsonParser(source);              // starting parsing of JSON String             while (parser.nextToken() != JsonToken.END_OBJECT) {                 String token = parser.getCurrentName();                  if ("firstname".equals(token)) {                     parser.nextToken();  //next token contains value                     String fname = parser.getText();  //getting text field                     System.out.println("firstname : " + fname);                  }                  if ("lastname".equals(token)) {                     parser.nextToken();                     String lname = parser.getText();                     System.out.println("lastname : " + lname);                  }                  if ("phone".equals(token)) {                     parser.nextToken();                     int telephone = parser.getIntValue();  // getting numeric field                     System.out.println("phone : " + phone);                  }                  if ("address".equals(token)) {                     System.out.println("address :");                     parser.nextToken(); // adjacent token volition live '[' which way JSON array                      // parse tokens until you lot uncovering ']'                     while (parser.nextToken() != JsonToken.END_ARRAY) {                         System.out.println(parser.getText());                     }                 }             }             parser.close();          } catch (JsonGenerationException jge) {             jge.printStackTrace();         } catch (JsonMappingException jme) {             jme.printStackTrace();         } catch (IOException ioex) {             ioex.printStackTrace();         }     } 


as well as hither is the output of our program, when you lot run it from Eclipse or withdraw from ascendency line of piece of employment :

Creating JSON file past times using Jackson Streaming API inwards Java JSON file created successfully done Parsing JSON file past times using Jackson Streaming API firstname : Garrison lastname : Paul telephone : 847332223 address : Unit - 232 Sofia Streat Bombay done


You volition besides run across file jacksondemo.json inwards your projection directory amongst next JSON String :

{   "firstname":"Garrison",   "lastname":"Paul",   "phone":847332223,    "address":["Unit - 232","Sofia Streat","Mumbai"] }


That's all most how to purpose Jackson Stream API to parse JSON String and to exercise JSON from Java object. It's a powerful library amongst lots of characteristic but Streaming is best. I know its picayune chip hard as well as you lot quest to write lot of code amongst hard coded filed names, it is the fastest way to read a large JSON file inwards Java amongst less retentiveness overhead. If you lot are dealing amongst normal size JSON output as well as you lot don't convey a retentiveness constraints as well as hence you lot tin e'er purpose Jackson Data binding model to parse JSON to Java Object.


Further Learning
Master Java Web Services as well as REST API amongst Spring Boot
REST API Design, Development & Management
tutorial)
  • 3 Ways to parse JSON String inwards Java? (tutorial)
  • How to convert JSON array to String array inwards Java? (example)
  • How to convert a Map to JSON inwards Java? (tutorial)
  • How to purpose Google Protocol Buffer inwards Java? (tutorial)
  • How to purpose Gson to convert JSON to Java Object? (example)
  • 5 Books to Learn REST as well as RESTful Web Services (books)

  • P.S. - If you lot are looking for online grooming to larn how to develop RESTful Web Services inwards Java using Spring framework, I advise you lot joining Eugen Paraschiv's REST amongst Spring course. The course of written report has diverse options depending upon your sense flat as well as how much you lot desire to larn e.g. beginner's class, intermediate class, as well as master copy class. You tin bring together the 1 which suits you lot better, though I advise joining the master class if you lot are serious most becoming an skillful Java REST developer.


    Sumber https://javarevisited.blogspot.com/

    0 Response to "Parsing Large Json Files Using Jackson Streaming Api Example"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel