0

I have a JSON file with no clue on how data will be in it nor the structure of data. The only thing known is that it will have either an array of JSON objects or a single JSON object.

I need to get each object from the file and store it as a separate item. In case of array of objects in the file, I should get an array of JSON strings which I can store in DB. Basically, I need to read this file and separate out each JSON object from it and store it in DB as a string.

One of the ways to do it was to use JACKSON ObjectMapper and assign these items to a Hashmap as key value pairs, but I am not sure though how it can be done If there are list of JSON Objects in the file.

Sample JSON File:

 [
   {
     "name":"Bob",
     "type":"Email",
     "from":"a@a.com",
     "to":"b@B.com",
     "attachments":[...],
     .
     .
     .
   }
 ]
Pang
  • 9,564
  • 146
  • 81
  • 122
Abhishek
  • 1,999
  • 5
  • 26
  • 52
  • Do you know the Object structure that the JSON has(let it be Array or a single one) ? – prasad vsv Mar 10 '15 at 15:13
  • so in short u don't know if it starts with '[' or '{' am I right. or could be the file may contains many seperated Json values? – nafas Mar 10 '15 at 15:13
  • I have updated my question. @nafas Yes, It might contain just one object or array of objects. – Abhishek Mar 10 '15 at 15:17
  • @prasadvsv : JSON Object Structure might not be consistent, It will change for each type of file, I have added a sample JSON – Abhishek Mar 10 '15 at 15:18
  • If you do not care about classes: use a Map. If you care about classes: make a schema parser that output java files. – Jazzwave06 Mar 10 '15 at 15:21

3 Answers3

2

Do you know the Object structure that the JSON has(let it be Array or a single one) ? If Yes,


First load the json string form the file into an in memory string.

  • check the string for Array existence, by searching for '[',']' in the outer structure of multiple occurrences of '{' or '}'
  • once you know whether you have an array or a single object, you can pass it as object reference to either Jackson or GSON parsers
  • create in memory Array of JsonObject.class say List. It is actually better to enclose this List inside another class. say myJsonObjects and have a List inside it.

Let us see GSON parsers (by google), though Jackson can also be used in the similar implementation

  Gson gson = new Gson();
  if(isArray){
     myJsonObjects  jsonArray = gson.fromJson(jsonStringFromFile,myJsonObjects );

  }
  else{
   gson.fromJson(jsonStringFromFile,JsonObject);
  }

http://google-gson.googlecode.com/svn-history/trunk/gson/docs/javadocs/com/google/gson/Gson.html

prasad vsv
  • 1,138
  • 8
  • 10
1

Jackson is my favorite JSON-to-POJO library. It doesn't really matter where you're loading the JSON from (a URL or from the filesystem), there are handlers for several input sources.

Here's an example:

Map<String,Object> userData = mapper.readValue(new File("user.json"), Map.class);

As far as having an unknown number of JSON structures that you're about to parse, the first thing that comes to mind is to have a mapper for each type you're expecting. You could then wrap the parsing code in try/catch blocks so that if the first fails with whatever exception Jackson gives you when encountering an unexpected format, you can then try the next format and so on.

If you're just trying to generically parse JSON that you don't know the structure of beforehand, you can try something like this:

mapper.readValue(jsonString, new TypeReference<List<EntryType>>() {});

The documentation for Jackson is pretty good-- giving it a solid read-through should definitely help. Here's a good five minute tutorial: http://wiki.fasterxml.com/JacksonInFiveMinutes

kwikness
  • 1,425
  • 4
  • 21
  • 37
  • Yes, I have used Jackson for predefined classes, and called objectMapper.readValue(file, List) which returns list of all student objects from file. But in this case, where I dont have a predefined class, Will mapper map only one object or array of objects? I am confused as to how will JACKSON give me back separated JSON objects from list of objects. Will each entry in Map "userData" contain a single JSON object? – Abhishek Mar 10 '15 at 15:20
  • Jackson supports mapping multiple objects as well. If you give the documentation a good read it should definitely clear things up for how mapping works. Here's a sample Mapper: http://wiki.fasterxml.com/JacksonSampleSimplePojoMapper If I understand your JSON structure correctly, it will always be those same keypairs and you can have a mapper that returns a list of their representative POJOs as well as a mapper for a single one. – kwikness Mar 10 '15 at 15:23
  • 1
    See the first answer here for an example: http://stackoverflow.com/questions/9829403/deserialize-json-to-arraylistpojo-using-jackson – kwikness Mar 10 '15 at 15:26
  • You would have to have implemented the Student class corresponding to the JSON structure. – kwikness Mar 10 '15 at 15:33
  • 1
    If you're just trying to generically parse JSON that you don't know the structure of beforehand, you can try something like the first answer here: http://stackoverflow.com/questions/18712495/gson-parse-generic-json-array – kwikness Mar 10 '15 at 15:35
  • 1
    Thanks for links, They helped me in fixing this issue – Abhishek Mar 10 '15 at 16:13
  • Any chance I can get best answer then? :-D – kwikness Mar 10 '15 at 17:31
0

I prefer use Gson:

Gson gson;
Map<String, Object>parameters=gson.fromJson(myString);

the rest is iterate the map, i hope help you