Skip to content
Advertisement

How to Parse Json containing Array of Arrays Java

I have a Json which contains array of Arrays, now I need to parse that Json and count the elements, and after reaching certain limit I need to put it into result Json. I was able to parse till one level and count the elements. How can I parse multiple levels and get the object in same format:

here is the sample code I tried for parsing one level with counting no.of elements:

JavaScript

and here is my Json :

JavaScript

And count Object is a Pojo which has offset and Limit variables , If I pass limit as 3 I should fetch only first 3 elements with same json format something like below :

JavaScript

Here I gave one of the sample JSON file, and Json can contain any no.of inner Array of elements, logic should be able to parse any type of Json. Here I should do the pagination as well for Json elements, means if I pass offSet and limit and I should fetch the elements accordingly. In the above example CountObject contains limit and offSet based on that it should fetch the elements. TO give more explanation If I pass offSet as 10 and limit a 10 I should fetch the elements in from 10th element to 20th element and so on.

Advertisement

Answer

Here is an approach using Jackson (I used version 2.11.1).

An “item” here is defined as one of the id/value pairs in the source JSON – for example:

JavaScript

I split the task into 2 parts:

  1. Cut off the data when the required limit is reached, by deleting subsequent items.

  2. Clean up any resulting empty objects or arrays.

Here is my input test data (based on the data provided in the question):

JavaScript

The code:

JavaScript

The reduce() method recursively iterates through the JSON, keeping track of the number of items collected – and then deletes any in excess of the required number.

However, this can leave empty [] arrays or {} objects in the JSON, so the stripEmpty() method handles that.

Because we are iterating sequentially through the JSON from top to bottom and from outer to inner, it’s possible that we may need more than one pass of the stripEmpty() method. There may be a more efficient approach, which only needs one pass, but this is approach is at least straightforward.

Examples of the results:

For limit = 2:

JavaScript

For limit = 1:

JavaScript

For limit = 0:

JavaScript

Additional Points:

Not Generic

The approach assumes there are never any arrays nested directly inside other arrays – so none of this: [ [ {...} ] ]. In other words, this is not a 100% generic parser, but does have some limitations in line with the sample data in the question.

Consider using POJOs

This solution does not define any POJO java objects into which the data is loaded – but it can often be easier to get what you want by doing that:

  • load (deserialize) the data into one or more POJOs.
  • remove unwanted data from the POJOs.
  • serialize the remaining data back to JSON.

If the example were any more complicated than the one in the question, I think I would favor doing this instead of manipulating only JsonNode data.

Update

Given the changes to the question, I think the best approach I can suggest is to parse each “item” (see definition above) into a POJO which would simply contain 3 fields:

JavaScript

The code to do this is as follows:

JavaScript

Instead of the println() statements, you would create a new instance of the POJO and add it to an ArrayList.

Now you have a standard list containing all your data – and you can access items 1 – 100, then 101 – 200… and so on, as needed for the user interface.

You would need to convert that raw POJO data back to whatever format the UI needs/expects, of course.

Using the example JSON from the question, the above approach prints this:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement