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

How to remove duplicate objects from json response object

Avatar

Avatar
Validate 1
Level 1
Helen_DD
Level 1

Likes

0 likes

Total Posts

6 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Applaud 5
View profile

Avatar
Validate 1
Level 1
Helen_DD
Level 1

Likes

0 likes

Total Posts

6 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Applaud 5
View profile
Helen_DD
Level 1

29-03-2021

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  

 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Applaud 25
Level 10
asutosh_jena
Level 10

Likes

593 likes

Total Posts

705 posts

Correct Reply

206 solutions
Top badges earned
Applaud 25
Give Back 100
Boost 500
Affirm 100
Ignite 1
View profile

Avatar
Applaud 25
Level 10
asutosh_jena
Level 10

Likes

593 likes

Total Posts

705 posts

Correct Reply

206 solutions
Top badges earned
Applaud 25
Give Back 100
Boost 500
Affirm 100
Ignite 1
View profile
asutosh_jena
Level 10

30-03-2021

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!

Answers (0)