0

Hi I am trying to compare fileds of two json objects and have to duplicates in json array.

below is my input which is json array of two json objects. both of the json objects have same tag called Architectural this has to be avoided and I want to make single json object with array of filenames without duplicates and filesize

in the array of json objects, each object may contain name json array with different length

   [         {
            "bucket": "vanasiri-100-98748-33434346-34545567002",
            "path": "1127/1854/1/BidSet/Architectural_Drawing/",
            "UpdatedBy": "100",                
            "fileSize": ["0"],
            "name": ["Display external company list.docx"],
            "tag": "Architectural",
            "uploadedDate": "03-30-2019"
            }, {
            "bucket": "vanasiri-100-98748-33434346-34545567002",
            "path": "1127/1854/1/BidSet/Architectural_Drawing/",
            "UpdatedBy": "100",               
            "fileSize": ["0"],
            "name": ["images.jfif"],
            "tag": "Architectural",
            "uploadedDate": "03-30-2019"
          }
]

Expected result

[{
     "bucket": "vanasiri-100-98748-33434346-34545567002",
     "path": "1127/1854/1/BidSet/Architectural_Drawing/",
     "UpdatedBy": "100",    
     "fileSize": ["0", "0"],
     "name": ["Display external company list.docx", "images.jfif"],
     "tag": "Architectural",
     "uploadedDate": "03-30-2019"
}]

How can i achieve this

mantelinga r
  • 440
  • 1
  • 5
  • 20
  • You can have your object model in java and use Jackson framework to convert from Json string to your Object. You can use technique to compare two objects, if they are same by tag name, you can finally form your object. – Sambit May 07 '19 at 08:03
  • I want to achieve this without a model class – mantelinga r May 07 '19 at 08:22

2 Answers2

1

Create a Model class say BucketData for the JSON Object inside your list and you can convert the JSON array to an Array List of Models.

Should looks something like below:

JSONArray jArray = (JSONArray)jsonObject; 
if (jArray != null) { 
   for (int i=0;i<jArray.length();i++){ 
    listdata.add(jArray.getString(i));
   } 
} 

Then you can perform Java 8 Filter operation to filter out the duplicate values as below:

List listdata = listdata.stream()
                    .collect(Collectors.collectingAndThen(
                            Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(BucketData:: getTag))),
                            ArrayList::new));

Referred from https://stackoverflow.com/a/17037364/4597596 and https://stackoverflow.com/a/55512294/4597596

Ganesh chaitanya
  • 638
  • 6
  • 18
0

In my opinion, you should first convert the JSON array into a SET; a HASHSET to be precise. HASHSET does not allow you to store a duplicate element.

Now, how to convert an array into hashset? please refer one of the older discussion on this forum - How to convert an Array to a Set in Java

vibhor
  • 13
  • 2