Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
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!