Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn more

View all

Sign in to view all badges

SOLVED

How to remove duplicate objects from json response object

Helen_DD
Level 2
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
asutosh_jena
Correct answer by
Community Advisor
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
asutosh_jena
Correct answer by
Community Advisor
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