Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

How to remove duplicate objects from json response object

Avatar

Level 2

Hi All,

JSONObject responseObj= {"pageinfo":[{"name":"page1", "path":"/content/proj/page1"} ,{"name":"page2","path":"/content/proj/page2"} , {"name":"page1", "path":"/content/proj/page1"} ]}

 

How to remove the duplicates?

I want responseObj as below

responseObj= {"pageinfo":[{"name":"page1", "path":"/content/proj/page1"} ,{"name":"page2","path":"/content/proj/page2"} ]}

 

Thanks in Advance

@AEM  

 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @Helen_DD 

You will need to convert the JSON to Java Objects and then perform the duplicate removal operation.

Added code snippet for each of the steps.

Please see below:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class Example {

@SerializedName("pageinfo")
@Expose
private List<Pageinfo> pageinfo = null;

public List<Pageinfo> getPageinfo() {
return pageinfo;
}

public void setPageinfo(List<Pageinfo> pageinfo) {
this.pageinfo = pageinfo;
}

}
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Pageinfo {

@SerializedName("name")
@Expose
private String name;
@SerializedName("path")
@Expose
private String path;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

}
Gson gson = new Gson(); // Create a GSON Object
Example exampleObj = gson.fromJson(initialResponse, Example.class); // Convert Json String to Gson Object
List<Pageinfo> pageinfoList = exampleObj.getPageinfo(); // Get the Page Info Details
TreeSet<Pageinfo> pageinfoTreeSet = pageinfoList.stream().collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Pageinfo::getName)))); // Remove the duplicate object using the 'name' field
exampleObj.setPageinfo(new ArrayList<>(pageinfoTreeSet)); // Set the Page Into back to List
String requiredResponse = gson.toJson(exampleObj); // Convert the list into Json String

Hope this helps!

Thanks!

View solution in original post

2 Replies

Avatar

Correct answer by
Community Advisor

Hi @Helen_DD 

You will need to convert the JSON to Java Objects and then perform the duplicate removal operation.

Added code snippet for each of the steps.

Please see below:

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class Example {

@SerializedName("pageinfo")
@Expose
private List<Pageinfo> pageinfo = null;

public List<Pageinfo> getPageinfo() {
return pageinfo;
}

public void setPageinfo(List<Pageinfo> pageinfo) {
this.pageinfo = pageinfo;
}

}
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Pageinfo {

@SerializedName("name")
@Expose
private String name;
@SerializedName("path")
@Expose
private String path;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPath() {
return path;
}

public void setPath(String path) {
this.path = path;
}

}
Gson gson = new Gson(); // Create a GSON Object
Example exampleObj = gson.fromJson(initialResponse, Example.class); // Convert Json String to Gson Object
List<Pageinfo> pageinfoList = exampleObj.getPageinfo(); // Get the Page Info Details
TreeSet<Pageinfo> pageinfoTreeSet = pageinfoList.stream().collect(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Pageinfo::getName)))); // Remove the duplicate object using the 'name' field
exampleObj.setPageinfo(new ArrayList<>(pageinfoTreeSet)); // Set the Page Into back to List
String requiredResponse = gson.toJson(exampleObj); // Convert the list into Json String

Hope this helps!

Thanks!