0

As part of the effective implementation of parse the multiple JSON's and aggregate the resultant JSON as final out.

Let suppose :

Json1 : 

[
{
  "id":"abc",
  "name" : "json"
},
... having 10k json objects
]

Json2: 

[
{
  "id":"abc",
  "language" : "java"
},
... having 10k json objects
]

Json3: 

[
{
  "id":"abc",
  "subject" : "solving"
},
... having 10k json objects
]

from the 3 jsons, requirements is 1. Optimised ways the search for the attribute "id" in 3 jsons and if match the map those json objects to final json object .

Approach followed

I tried in the following way

Iterate over the first JSON array object and find the one attribute "id" and iterate over remaining JSON's and look the matching "id" and corresponding JSON object merging and forming final objects 
In this process, 
Taking O(n^3) time for search the find the matching records 
more ever n > 10k in my case  


What will be the best approach to proceed

  • Yes, your approach seems ok, if you don't know where in stream (json) THE id are, you need to stream them one by one (to find all), there is no way around, for how to stream see this for example https://stackoverflow.com/q/25064773/5292302 – Petter Friberg Nov 02 '19 at 11:09
  • But in this approach costs auxiliary space which impact to application down. Do we have any existing libraries or patterns to achieve this – praveen kumar bommali Nov 02 '19 at 13:37
  • I don't know of others, mapping all to objects and them check however is slower and much more memory consuming – Petter Friberg Nov 02 '19 at 17:03

1 Answers1

0

Assuming you already know all the possible JSON fields and id is a unique attribute, create a java object to reflect all the fields.

class MyObject{
    String id;
    String name; 
    String language;
    String subject;
}

Then create a list of key value pair, with id as the key. And whenever you find an id, if it is present in the list of key-value pair then modify the corresponding object. Otherwise create a new object and add it as a new key-value pair to the list. With this you'll be traversing each JSON only once but it might take a lot of auxiliary space.