Written By:Brian Geary
June 6, 2018
Category:Languages Software Development
As a programmer I have found the big drawback with JSON comes when either you do not know the JSON structure, the JSON has a variety of structures, the JSON has hundreds of fields in nested structures, or combinations of all three. When the JSON data is “neat” the job of reading or parsing can be trivial. However when fields are missing, have been added, or the cardinality has changed then things get messier. There are some tools that cannot process JSON when fields are missing, appear unexpectedly, or have a different JSON type.
To solve these problems I have used “jsonschema2pojo” by “joelittlejohn.” Jsonschema2pojo takes a JSON document and creates Plain Old Java Objects (POJO) out of the JSON. Once you have the Java you can instantiate future JSON files in Java and process, modify, extract, validate, and use your data. Jsonschema2pojo is also tolerant of fields that are missing, new unexpected fields, and you can also handle different JSON types within Java using an override method, and if you know the cardinality sometimes changes you can also make those changes in Java.
The basic steps to use jsonschema2pojo is to do the following. First, either find a JSON file that is representative of your data. If you do not have one you may need to create one. You want an input JSON file that has all the fields that you know about and with the cardinality you know about. Second, you need to use jsonschema2pojo to make the POJOs. Third, you will need to create a Java class (or set of classes) to process the data. You then need to run process your data against all your POJOs and as you encounter errors (you will likely if your data is messy or complex) and then either modify the Java code or modify your input file.
To try it yourself please download the Java and input files provided with the following links.
To download the java files click here (contains README.txt)
To download the input files click here
Reference: https://github.com/<wbr />joelittlejohn/jsonschema2pojo/<wbr />wiki/Getting-Started#using-<wbr />jsonschema2pojo-within-your-<wbr />java-project-embedded