Insert and Get operations on JSON object in MongoDB with Spring Boot - java

How can I save JSON object with different level of hierarchies in MongoDB ?
For example :-JSON
{
"name": "abc",
"password": "xyz",
"address": {
"street": "ghgjk",
"pin": 25646
},
"readingHabbits": [
"jkjsdj",
"sdkhks",
"jlcsd"
],
"eatingHabbits": {
"internalObjOne": {
"dkks": "jdskdfl",
"lfld": "hfslvlsk"
},
"internalObjectSecond": {
"cjdlksl": "hcdkjnjkcs",
"cjsdjljsl": "chsdskjc"
}
}
}
How can I store above JSON values into MongoDB in different Collections using SpringBoot and Java?
Also how can I get the same result when I do repository.findAll();

It should look like this:
db.getCollection("images").insertOne(
new Document("name", "abc")
.append("password", "xyz")
.append("address",
new Document()
.append("street", "ghgjk")
.append("pin", 25646))
.append("readingHabbits", Arrays.asList("jkjsdj", "sdkhks", "jlcsd"))
.append("eatingHabbits", Arrays.asList(
new Document()
.append("internalObjOne",
new Document()
.append("dkks", "jdskdfl")
.append("lfld", "hfslvlsk")),
new Document()
.append("internalObjectSecond",
new Document()
.append("cjdlksl", "hcdkjnjkcs")
.append("cjsdjljsl", "chsdskjc"))
)
)
);

thanks for the answer, But I think I didn't made myself clear.
Suppose I have a json object.
{ "name": "abc", "password": "xyz", "address": { "street": "ghgjk", "pin": 25646 }, "readingHabbits": [ "jkjsdj", "sdkhks", "jlcsd" ], "eatingHabbits": { "internalObjOne": { "dkks": "jdskdfl", "lfld": "hfslvlsk" }, "internalObjectSecond": { "cjdlksl": "hcdkjnjkcs", "cjsdjljsl": "chsdskjc" } } }
Base class :-
#Document(collection="user")
public class User{
#Id
private String id;
private String name;
private String password;
#DBRef
private Address address;
#DBRef
private ReadingHabbits readingHabbits;
//setter and getter
}
#Document(collection="address")
public class Address{
#Id
private String id;
private String street;
private int pin;
//setter and getter
}
Do we need to create separate classes for "ReadingHabbits" and "EatingHabbits" ?
If not then is the above approach correct to map two different entities with DBRef and save it to two different collection in MongoDB?
Or do we have any other way to do the same operation ?
----------

Related

Aggregation Java MongoDB with MongoTemplate not return group value

In my Spring Controller I cannot figure out how to retrieve correctly the result of a MongoDB with MongoTemplate aggregation. Below is the code from my Controller class:
Aggregation aggregation = Aggregation.newAggregation(
Aggregation.match(Criteria.where("modality").is("check")),
Aggregation.group("name").count().as("check_count"),
Aggregation.project("name").and("check_count").as("check_count")
);
AggregationResults<Response> res = mongoTemplate.aggregate(aggregation, "user", Response.class);
Below the simple class of User and Response:
#Document(collection = "user")
class User{
#Id
private String id;
private String name;
private String modality;
//constructor and get/set
}
class Response{
private String name;
private string check_count;
//constructor and get/set
}
So I retrieve correctly my response but I do not see the name, that is always null:
{
"user": [
{
"name": null,
"check_count": 61
},
{
"name": null,
"check_count": 15
},...
What is wrong in my Aggregation.group ? Thanks
When you do the group aggregation, the pipeline that gets generated is mostly
{ "$group" : { "_id" : "$name", "check_count" : { "$sum" : 1}}}
So, the result of the group stage will have _id as the field, not name.
Subsequent, stage should use _id field.
Aggregation.project().and("_id").as("name").and("check_count").as("check_count");

Creating the Base Entity class in relational entity classes

The json below is what was sent to me, when I findByPinCode. I have created relations between them in the following accordance with the json answer. Here are the relationships.
{
"RequestIdentifier": "00000000-0000-0000-0000-000000000000",
"Status": {
"Name": "string",
"Code": 0,
"Message": "string"
},
"Response": {
"Person": {
"Pin": "string",
"LastName": "string",
"FirstName": "string",
"FatherName": "string",
"BirthDate": "string"
},
"Company": {
"Voen": "string",
"Name": "string",
"PersonType": "string",
"Persons": [
{
"Pin": "string",
"LastName": "string",
"FirstName": "string",
"FatherName": "string",
"BirthDate": "string"
}
]
},
"Farms": [
{
"Id": 0,
"Name": "string",
"Fields": [
{
"VillageName": "string",
"DocType": "string",
"FieldAmount": 0,
"Unit": "string",
"Plants": [
{
"Name": "string",
"FieldAmount": 0,
"Unit": "string"
}
]
}
],
"Animals": [
{
"Sort": "string",
"Count": 0
}
],
"Bees": [
{
"Sort": "string",
"Count": 0
}
]
}
]
}
}
(One)Company To (Many)Persons
(One)FarmInfo To (Many)FarmFields
(One)FarmFields To (Many)Plants
(One)FarmInfo To (Many)Animals
(One)FarmInfo To (Many)Bees
and thus converting them to my dto class first. Then I want to convert them to my entity classes and persist them completely to the database.
This is my DTO class.
#Data
#NoArgsConstructor
#AllArgsConstructor
public class FarmInfoMainResult {
#JsonProperty(value = "RequestIdentifier")
private String requestIdentifier;
#JsonProperty(value = "Status")
private ResponseStatus status;
#JsonProperty(value = "Response")
private FarmInfoData response;
}
and this is my business logic layer of method, which I want to that in here convertint the dto class to the BaseEntity class and at the end of I want to persist BaseEntity class to the database.
#Override
public ResponseEntity<FarmInfoMainResult> findFarmInfoByPin(String pin, String username, String branchCode) {
String requestIdentifier = UUID.randomUUID().toString();
ResponseEntity<FarmInfoMainResult> farmInfoPinServiceResponse = clientUtility.getForObject(farmInfoForPinUrl + pin, requestIdentifier, FarmInfoMainResult.class);
System.out.println("farmInfoPinServiceResponse " + farmInfoPinServiceResponse.getBody());
/*
Here are I want to convert farmInfoPinServiceResponse variable to the my Base Entity class and so persist it to the database, so that
thus, values will be automatically added to them as they are related to each other in other relational databases.
*/
return farmInfoPinServiceResponse;
}
#Entity
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
public class BaseEntity {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private String requestIdentifier;
#OneToOne(cascade = CascadeType.ALL)
private Status status;
#OneToOne(cascade = CascadeType.ALL)
private Response response;
}
But I am getting the following error in my Response class. basic' attribute type should not be a container
#Entity
#Data
#Builder
#NoArgsConstructor
#AllArgsConstructor
public class Response {
#Id
#GeneratedValue(strategy = GenerationType.SEQUENCE)
private Long id;
private PersonFarmInfo personFarmInfo;
private CompanyFarmInfo companyFarmInfo;
private FarmInfo farmInfo;
private FarmFieldInfo farmFieldInfo;
private FarmPlantInfo farmPlantInfo;
private FarmAnimalInfo farmAnimalInfo;
private FarmBeeInfo farmBeeInfo;
#OneToOne(mappedBy = "response")
private BaseEntity baseEntity;
}
If I don't create main entity class, there will be a situation like this. I will add the properties of each class that is in the dto to the properties of each of my entity class and print them to the database. But by insert to a single entity class I want others to be affected as well. I wanted to solve the issue by creating a main entity class like this, but I'm getting the following error.
As I said at the beginning, the codes below do this by separating them into parts. These codes my old version codes.
#Override
public ResponseEntity<FarmInfoMainResult> findFarmInfoByPin(String pin, String username, String branchCode) {
String requestIdentifier = UUID.randomUUID().toString();
ResponseEntity<FarmInfoMainResult> farmInfoPinServiceResponse = clientUtility.getForObject(farmInfoForPinUrl + pin, requestIdentifier, FarmInfoMainResult.class);
System.out.println("farmInfoPinServiceResponse " + farmInfoPinServiceResponse.getBody());
saveFarmInfoPin(farmInfoPinServiceResponse.getBody(), username, branchCode, pin);
return farmInfoPinServiceResponse;
}
Here I am sending the object stored in dto to my saveFarmInfoPin method.Then this method works. Here, my method saves all my object values stored in dto separately.
private void saveFarmInfoPin(FarmInfoMainResult farmInfoMainResult, String username, String branchcode, String pin) {
// try {
if (farmInfoMainResult.getResponse() != null) {
saveFarmInfoPersonPin(farmInfoMainResult.getResponse().getPerson(), farmInfoMainResult.getRequestIdentifier(), username, branchcode, pin);
saveFarmInfoCompanyPin(farmInfoMainResult.getResponse().getCompany(), farmInfoMainResult.getRequestIdentifier(), username, branchcode);
saveFarminfo(farmInfoMainResult.getResponse().getFarms());
/* } catch (Exception e) {
e.printStackTrace();
}
*/
}
}
Finally, my following methods divide each of my dto objects into parts, first converting them to my entity class, and then adding them to the database via repository.
private void saveFarmInfoPersonPin(FarmInfoPerson farmInfoPersonDto, String requestIdentifier, String username, String branchCode, String pin) {
if (farmInfoPersonDto != null) {
FarmPersonInfo newFarmInfoPerson = new FarmPersonInfo();
newFarmInfoPerson.setRequestIdentifier(requestIdentifier);
newFarmInfoPerson.setBranchCode(branchCode);
newFarmInfoPerson.setUsername(username);
newFarmInfoPerson.setPin(pin);
newFarmInfoPerson.setPin(farmInfoPersonDto.getPin());
newFarmInfoPerson.setFatherName(farmInfoPersonDto.getFatherName());
newFarmInfoPerson.setFirstName(farmInfoPersonDto.getFirstName());
newFarmInfoPerson.setBirthDate(farmInfoPersonDto.getBirthDate());
personFarmInfoRepository.save(newFarmInfoPerson);
}
}
private void saveFarmInfoCompanyPin(FarmInfoCompany farmInfoCompany, String requestIdentifier, String username, String branchCode) {
List<FarmPersonInfo> newFarmInfoPersonList = new ArrayList<>();
if (farmInfoCompany != null) {
FarmCompanyInfo newCompanyFarmInfo = new FarmCompanyInfo();
newCompanyFarmInfo.setName(farmInfoCompany.getName());
newCompanyFarmInfo.setRequestIdentifier(requestIdentifier);
newCompanyFarmInfo.setUsername(username);
newCompanyFarmInfo.setBranchCode(branchCode);
newCompanyFarmInfo.setPersonType(farmInfoCompany.getPersonType());
newCompanyFarmInfo.setVoen(farmInfoCompany.getVoen());
// CompanyFarmInfo companyFarmInfo = companyFarmInfoRepository.save(newCompanyFarmInfo);
for (FarmInfoPerson farmInfoPerson : farmInfoCompany.getPersons()) {
FarmPersonInfo personInfo = new FarmPersonInfo();
personInfo.setPin(farmInfoPerson.getPin());
personInfo.setFatherName(farmInfoPerson.getFatherName());
personInfo.setFirstName(farmInfoPerson.getFirstName());
personInfo.setBirthDate(farmInfoPerson.getBirthDate());
personInfo.setFarmCompanyInfo(newCompanyFarmInfo);
newFarmInfoPersonList.add(personInfo);
}
personFarmInfoRepository.saveAll(newFarmInfoPersonList);
}
}
private void saveFarminfo(List<FarmInfoFarm> farmInfoFarms) {
List<FarmFieldInfo> newFarmFieldInfoList = new ArrayList<>();
List<FarmAnimalInfo> newFarmAnimalInfoList = new ArrayList<>();
List<FarmBeeInfo> newFarmBeeInfoList = new ArrayList<>();
List<FarmPlantInfo> newFarmPlantInfoList = new ArrayList<>();
for (FarmInfoFarm farmInfoFarm : farmInfoFarms) {
FarmInfo newFarmInfo = new FarmInfo();
newFarmInfo.setName(farmInfoFarm.getName());
newFarmInfo.setFarmInfoId(farmInfoFarm.getId());
FarmInfo farmInfo = farmInfoRepository.save(newFarmInfo);
for (FarmInfoField farmInfoField : farmInfoFarm.getFields()) {
FarmFieldInfo newFarmFieldInfo = new FarmFieldInfo();
newFarmFieldInfo.setVillageName(farmInfoField.getVillageName());
newFarmFieldInfo.setDocType(farmInfoField.getDocType());
newFarmFieldInfo.setFieldAmount(farmInfoField.getFieldAmount());
newFarmFieldInfo.setUnit(farmInfoField.getUnit());
newFarmFieldInfo.setFarmInfo(farmInfo);
newFarmFieldInfoList.add(newFarmFieldInfo);
for (FarmInfoPlant farmInfoPlant : farmInfoField.getPlants()) {
FarmPlantInfo newfarmPlantInfo = new FarmPlantInfo();
newfarmPlantInfo.setName(farmInfoPlant.getName());
newfarmPlantInfo.setFieldAmount(farmInfoPlant.getFieldAmount());
newfarmPlantInfo.setUnit(farmInfoPlant.getUnit());
newfarmPlantInfo.setFarmFieldInfo(newFarmFieldInfo);
newFarmPlantInfoList.add(newfarmPlantInfo);
}
farmPlantInfoRepository.saveAll(newFarmPlantInfoList);
}
farmFieldInfoRepository.saveAll(newFarmFieldInfoList);
for (FarmInfoAnimal farmInfoAnimal : farmInfoFarm.getAnimals()) {
FarmAnimalInfo newFarmAnimalInfo = new FarmAnimalInfo();
newFarmAnimalInfo.setCount(farmInfoAnimal.getCount());
newFarmAnimalInfo.setSort(farmInfoAnimal.getSort());
newFarmAnimalInfo.setFarmInfo(farmInfo);
newFarmAnimalInfoList.add(newFarmAnimalInfo);
}
farmAnimalInfoRepository.saveAll(newFarmAnimalInfoList);
for (FarmInfoBee farmInfoBee : farmInfoFarm.getBees()) {
FarmBeeInfo newfarmBeeInfo = new FarmBeeInfo();
newfarmBeeInfo.setCount(farmInfoBee.getCount());
newfarmBeeInfo.setSort(farmInfoBee.getSort());
newfarmBeeInfo.setFarmInfo(farmInfo);
newFarmBeeInfoList.add(newfarmBeeInfo);
}
farmBeeInfoRepository.saveAll(newFarmBeeInfoList);
}
}
But as I said, instead of separating them all into separate-parts, can I add all of these objects to my main entity class and bring all these particles together through just one method?
I'm thinking of something like this, now I will use jackson to convert all the values in the json value I have received to the classes it belongs to, then I will add them to my main class and finally add my main class to my database. Since the main class will already have all the relations in itself, I think that all of them will be affected by doing this operation once.

Populate List inside List With Conditionals Java

I'm trying to populate a List<Object> that has as a attribute another List.
Here is The Scenario.
First Object
public class Student {
private String initials;
private String id;
private List<StudentDetail> listStudentDetail;
//Setters and Getters
}
Second Object
public class StudentDetail {
private String id;
private String name;
//Setters and getters
}
My final java object is as follows.
public class Response {
private String code;
private String message;
private List<Student> listStudent;
//Setters and getters
}
I get populated a List<Student>and a List<StudentDetail> from another process and what I want to achive is merge both list into my Response class but with theese particular conditions
In order to fill List<StudentDetail>
Student.initials must be "a"
Student.id = StudentDetail.id
These is what I want to Achieve.
{
"code": "0",
"message": "Succes",
"listStudent": [
{
"initials": "a",
"id": "104",
"listStudentDetail": [
{
"id": "104",
"name": "Kevin"
}
]
},
{
"initials": "b",
"id": "100",
"listStudentDetail": []
},
{
"initials": "a",
"id": "105",
"listStudentDetail": [
{
"id": "105",
"name": "Robert"
}
]
}
]
}
Here is the code.
I am assuming populatedStudentList and populatedStudentDetail as the given objects that have been populated in some other part of the code like you mentioned.
for (Student s in populatedStudentList){
if (s.getInitials().compareTo('a') != 0){
continue;
}
String findId = s.getId();
List<StudentDetail> tempListStudentDetail = new ArrayList<>();
for (StudentDetail d in populatedStudentDetail){
if (d.getId().compareTo(findId) == 0){
tempListStudentDetail.append(d);
}
}
s.setListStudentDetail(tempListStudentDetail);
}
This can be achieved in a more memory efficient manner using hashTables but I have tried to reproduce the desired result in the most basic manner.

group by a field in Java Streams

So, I have an input JSON that looks like this:
[{
"added": "2014-02-01T09:13:00Z",
"author": {
"id": "1",
"name": "George R R Martin",
"added_on": "2013-02-01T09:13:00Z"
},
"book": {
"id": "12",
"name": "Game of Thrones",
"genre": "Fantasy Fiction"
}
},
{
"added": "2015-02-01T09:13:00Z",
"author": {
"id": "2",
"name": "Patrick Rothfuss",
"added_on": "2012-09-13T011:40:00Z"
},
"book": {
"id": "15",
"name": "The Name of the Wind",
"genre": "Fantasy Fiction"
}
}, {
"added": "2016-02-01T09:13:00Z",
"author": {
"id": "2",
"name": "Patrick Rothfuss",
"added_on": "2012-09-13T011:40:00Z"
},
"book": {
"id": "17",
"name": "The Wise Man's Fear",
"genre": "Fantasy Fiction"
}
}]
I need to group it basis on author.id. An author will have one object and a list of all the books he's authored.
This is what I expect the output:
[
{
"author": "George R R Martin",
"added_on": "2013-02-01T09:13:00Z",
"books": [
{
"book_name": "Game of Thrones",
"added": "2014-02-01T09:13:00Z"
}
]
},
{
"author": "Patrick Rothfuss",
"added_on": "2012-09-13T011:40:00Z",
"books": [
{
"book_name": "The Name of the Wind",
"added": "2015-02-01T09:13:00Z"
}, {
"book_name": "The Wise Man's Fear",
"added": "2016-02-01T09:13:00Z"
}
]
}
]
I tried doing it through a normal for loop -- it works. But, just for the sake of learning more about Streams, I want to try it out using Streams.
I tried this:
Map<Author, List<Book>> collect = authorsList.stream()
.collect(Collectors.groupingBy(AuthorBookObj::getAuthor,
Collectors.mapping(AuthorBookObj::getBook, Collectors.toList())));
But, didn't get what I needed. Instead, it created three Maps instead of two.
Also tried this:
Map<AuthorTuple, List<Book>> collect = authorsList.stream()
.collect(Collectors.groupingBy(authors -> new AuthorTuple(authors.getAuthor().getId(),
authors.getAuthor().getName(), authors.getAuthor().getAddedOn()),
Collectors.mapping(AuthorBookObj::getBook, Collectors.toList())));
It also gives me three objects in the list. I expected to have two authors and corresponding books for each author.
AuthBookObj:
public class AuthorBookObj
{
private String id;
private Author author;
private Book book;
private String added;
//getter, setter
}
public class Article
{
private String name;
private String id;
private String genre;
}
public class Author
{
private String name;
private String added_on;
private String id;
}
The problem is not the way you handle the stream, it is in the equality of the objects.
The correct way is to use this code:
Map<Author, List<Book>> collect = authorsList.stream()
.collect(Collectors.groupingBy(AuthorBookObj::getAuthor,
Collectors.mapping(AuthorBookObj::getBook, Collectors.toList())));
But now you are comparing Author objects, since the objects are different you get three entries. You need to add a hashcode and equals in the Author object that will compare the objects on the author id.
//code generated from intellij.
// Author.java
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Author author = (Author) o;
return getId() == author.getId();
}
#Override
public int hashCode() {
return Objects.hash(getId());
}
If you don't have limitation on creating new POJO classes on requirement, i will do in this way
First to parse the input JSON to java object
Response class with AuthorDetails and BookDetails class
class Response {
private String addedOn;
private AuthorDetails author;
private BookDetails book;
}
AuthorDetails
class AuthorDetails {
private String id;
private String name;
private String addedOn;
}
BookDetails
class BookDetails {
private String id;
private String name;
private String gener;
}
And i will map the input json to List<Response>
List<Response> list = Arrays.asList(new Response());
Then now converting List<Response> into desired output i have added couple of POJO classes
AuthorAndBooks
class AuthorAndBooks {
#JsonProperty("author")
private String author;
#JsonProperty("added_on")
private String addedOn;
#JsonProperty("books")
List<AuthorBooks> books;
}
AuthorBooks
class AuthorBooks {
#JsonProperty("book_name")
private String name;
#JsonProperty("added")
private String added;
}
Now do group by based on author name
Map<String, List<Response>> group = list.stream().
collect(Collectors.groupingBy(res->res.getAuthor().getName()));
And now for every Author add the books
List<AuthorAndBooks> authorBooks = group.entrySet().stream().
map(entry->{
AuthorAndBooks ab = new AuthorAndBooks();
ab.setAuthor(entry.getKey());
ab.setAddedOn(entry.getValue().stream().findFirst().get().getAddedOn());
ab.setBooks(entry.getValue().stream().map(authorBook->{
AuthorBooks books = new AuthorBooks();
books.setName(authorBook.getBook().getName());
books.setAdded(authorBook.getAddedOn());
return books;
}).collect(Collectors.toList()));
return ab;
}).collect(Collectors.toList());
First of all want to pay attention to "added" field from input JSON. What does this belong to? I guess it belongs to Book object. If so it would be good to place this field (if it possible) inside Book object. Then you need to deserialize this json to java objects. It can be done by com.fasterxml.jackson.databind.ObjectMapper But you can use any json framework for this.
ObjectMapper mapper = new ObjectMapper();
AuthorBookObj[] objs = mapper.readValue(inputJson, AuthorBookObj[].class);
Then you need to group these objects and your first solution is well suited:
Map<Author, List<Book>> collect = Arrays.stream(objs)
.collect(groupingBy(AuthorBookObj::getAuthor,
mapping(AuthorBookObj::getBook, toList())));
How it was mentioned in previous answer you need to make sure there are equals/hashcode methods in your class that is used for as key in Map (In this case Author). The main confuse now is that desirable json output doesn't represent Map. It is just list of some custom object with fields like author, added_on, books which is list also.
So to achieve this goal you need to transform your Map<Author, List<Book>> to list of custom objects. For example:
public class PublicationInfo {
private String author;
private String added_on;
private List<BookBriefInfo> books;
...
}
public class BookBriefInfo {
private String book_name;
private String added;
...
}
List<PublicationInfo> infos = new ArrayList<>();
for (Map.Entry<Author, List<Book>> entry : collect.entrySet()) {
PublicationInfo info = new PublicationInfo();
info.setAuthor(entry.getKey().getName());
info.setAdded_on(entry.getKey().getAdded_on());
List<BookBriefInfo> bookInfos = new ArrayList<>();
for (Book book : entry.getValue()) {
bookInfos.add(new BookBriefInfo(book.getBook_name(), book.getAdded()))
}
info.setBooks(bookInfos);
}
Finally it can be serialized:
String jsonResult = mapper.writeValueAsString(infos);
By the way, to get json output formatted just configure it:
mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
You must override equals and hashCode. If you fail to do so, your class will violate the general contract for hashCode, which will prevent it from functioning properly in collections such as HashMap and HashSet. The Author class’s failure to override hashCode causes the two equal instances to have unequal hash codes, in violation of the hashCode contract. Add this to your Author class.
#Override
public int hashCode() {
return id.hashCode();
}
#Override
public boolean equals(Object obj) {
return obj instanceof Author && ((Author) obj).getId().equals(id);
}
With that in place, the following code snippet should work as expected.
Map<Author, List<Article>> booksByAuthor = authorsList.stream()
.collect(Collectors
.groupingBy(AuthorBookObj::getAuthor,
Collectors.mapping(AuthorBookObj::getBook, Collectors.toList())));

JSON Array of objects to plain old java object

this is my first time making an external api call in Java, so please bear with me as I'm not very experienced. I got the http request working and got a response, but now I need to parse it.
I'm trying to convert a json array to java objects. I understand the gist of it, but all examples I've seen don't apply to my issue.
I need the 'entities' objects from the json string. The details (which are an array, too) can contain any key/value pair, so I was thinking of putting that in a hashmap in each Entity object. I've tried the gson library, but I can't find any gson example that goes deeper than a single dimensional json array.
I realize this is kind of a broad question, and I don't expect anyone to deliver me a working solution, but a few tips or a link to a relevant guide would go a long way. :)
{
"return": {
"entities": [
{
"id": 2385,
"details": [
{
"name": "Other Known Name",
"value": "John Wick",
"match": false
}
],
"proofs": [],
"link": "http://domain.gg/users?id=2385"
},
{
"id": 2384,
"details": [
{
"name": "Discord ID",
"value": "159985870458322944",
"match": false
},
{
"name": "SteamID64",
"value": "76561197991558078",
"match": true
},
{
"name": "SteamVanity",
"value": "test",
"match": false
},
{
"name": "PS4",
"value": "John_S",
"match": false
},
{
"name": "XBox",
"value": "John S",
"match": false
},
{
"name": "Email",
"value": "john_smith#gmail.com",
"match": true
},
{
"name": "Comment",
"value": "Test user",
"match": false
},
{
"name": "Other Known Name",
"value": "Jonathan",
"match": false
},
{
"name": "Reddit",
"value": "/u/johns",
"match": true
}
],
"proofs": [],
"link": "http://domain.gg/users?id=2384"
},
{
"id": 1680,
"details": [
{
"name": "Other Known Name",
"value": "Johny",
"match": false
},
{
"name": "SteamID64",
"value": "76561198213003675",
"match": true
}
],
"proofs": [],
"link": "http://domain.gg/users?id=1680"
},
{
"id": 1689,
"details": [
{
"name": "Other Known Name",
"value": "JohnnyPeto",
"match": false
},
{
"name": "SteamID64",
"value": "76561198094228192",
"match": true
}
],
"proofs": [],
"link": "http://domain.gg/users?id=1689"
}
],
"notice": "Showing 4 out of 4 matches."
}
}
There are many json serialization/deserialization frameworks available. I would recommend having a look at Jackson.
Basically, you have to create Model corresponding to json schema and deserialize json into object. Based on the example in the question, model will look like this:
#JsonIgnoreProperties(ignoreUnknown = true)
class Response {
#JsonProperty("return")
private ResponseObject responseObject;
public ResponseObject getResponseObject() {
return responseObject;
}
public void setResponseObject(ResponseObject responseObject) {
this.responseObject = responseObject;
}
}
#JsonIgnoreProperties(ignoreUnknown = true)
class ResponseObject {
private List<Entity> entities;
public List<Entity> getEntities() {
return entities;
}
public void setEntities(List<Entity> entities) {
this.entities = entities;
}
}
#JsonIgnoreProperties(ignoreUnknown = true)
class Entity {
private String id;
private List<Details> details;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Details> getDetails() {
return details;
}
public void setDetails(List<Details> details) {
this.details = details;
}
}
#JsonIgnoreProperties(ignoreUnknown = true)
class Details {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Once the model is defined, you can use ObjectMapper class to perform serialization/deserialization, e.g.:
ObjectMapper mapper = new ObjectMapper();
Response response = mapper.readValue("{\"return\": {\"entities\": [{\"id\": 2385,\"details\": [{\"name\": \"Other Known Name\",\"value\": \"John Wick\",\"match\": false}],\"proofs\": [],\"link\": \"http://domain.gg/users?id=2385\"},{\"id\": 2384,\"details\": [{\"name\": \"Discord ID\",\"value\": \"159985870458322944\",\"match\": false},{\"name\": \"SteamID64\",\"value\": \"76561197991558078\",\"match\": true},{\"name\": \"SteamVanity\",\"value\": \"test\",\"match\": false},{\"name\": \"PS4\",\"value\": \"John_S\",\"match\": false},{\"name\": \"XBox\",\"value\": \"John S\",\"match\": false},{\"name\": \"Email\",\"value\": \"john_smith#gmail.com\",\"match\": true},{\"name\": \"Comment\",\"value\": \"Test user\",\"match\": false},{\"name\": \"Other Known Name\",\"value\": \"Jonathan\",\"match\": false},{\"name\": \"Reddit\",\"value\": \"/u/johns\",\"match\": true}],\"proofs\": [],\"link\": \"http://domain.gg/users?id=2384\"},{\"id\": 1680,\"details\": [{\"name\": \"Other Known Name\",\"value\": \"Johny\",\"match\": false},{\"name\": \"SteamID64\",\"value\": \"76561198213003675\",\"match\": true}],\"proofs\": [],\"link\": \"http://domain.gg/users?id=1680\"},{\"id\": 1689,\"details\": [{\"name\": \"Other Known Name\",\"value\": \"JohnnyPeto\",\"match\": false},{\"name\": \"SteamID64\",\"value\": \"76561198094228192\",\"match\": true}],\"proofs\": [],\"link\": \"http://domain.gg/users?id=1689\"}],\"notice\": \"Showing 4 out of 4 matches.\"}}", Response.class);
System.out.println(response.getResponseObject().getEntities().get(0).getId());
Here's the Javadoc.
If I were you, I'd use Jackson, not GSON. It's specialized on JavaBeans-style mapping. Write classes like this:
public class Detail{
private String name;
private String value;
private boolean match;
// + getters / setters
}
public class Entity{
private int id;
private List<Detail> details;
private String link;
private List<String> proofs;
// you don't have any example data for this, so I'm assuming strings
// + getters / setters
}
public class Result{
private List<Entity> entities;
private String notice;
// + getters / setters
}
and do the conversion with something like
Result result = new ObjectMapper().readValue(json, Result.class);
As my fellow stackoverflow users have previously posted, for this kind of initilization Jackson API would be better. I have however posted the solution for your question with Gson.
I noticed that you like your details to be stored as a HashMap with id as key. However, it seems like this id is actually related to the entities and not to the details.
Disclaimer, I got lazy and used an online POJO generator because I did not want to create objects for all of the Json elements ;) It still showcases how it should be done:
class Main{
public static void main(String[] args) throws FileNotFoundException {
//this is just to load the json file
String input = new Scanner(new File("test.txt")).useDelimiter("\\Z").next();
System.out.println(input);
Gson gson = new Gson();
Example arr = gson.fromJson(input, Example.class);
System.out.println(arr);
}
public class Detail {
#SerializedName("name")
#Expose
public String name;
#SerializedName("value")
#Expose
public String value;
#SerializedName("match")
#Expose
public Boolean match;
#Override
public String toString() {
return "Detail [name=" + name + ", value=" + value + ", match=" + match + "]";
}
}
public class Entity {
#SerializedName("id")
#Expose
public Integer id;
#SerializedName("details")
#Expose
public List<Detail> details = null;
#SerializedName("proofs")
#Expose
public List<Object> proofs = null;
#SerializedName("link")
#Expose
public String link;
#Override
public String toString() {
return "Entity [id=" + id + ", details=" + details + ", proofs=" + proofs + ", link=" + link + "]";
}
}
public class Example {
#SerializedName("return")
#Expose
public Return _return;
#Override
public String toString() {
return "Example [_return=" + _return + "]";
}
}
public class Return {
#SerializedName("entities")
#Expose
public List<Entity> entities = null;
#SerializedName("notice")
#Expose
public String notice;
#Override
public String toString() {
return "Return [entities=" + entities + ", notice=" + notice + "]";
}
}
}
Output
Example [_return=Return [entities=[Entity [id=2385, details=[Detail [name=Other Known Name, value=John Wick, match=false]], proofs=[], link=http://domain.gg/users?id=2385], Entity [id=2384, details=[Detail [name=Discord ID, value=159985870458322944, match=false], Detail [name=SteamID64, value=76561197991558078, match=true], Detail [name=SteamVanity, value=test, match=false], Detail [name=PS4, value=John_S, match=false], Detail [name=XBox, value=John S, match=false], Detail [name=Email, value=john_smith#gmail.com, match=true], Detail [name=Comment, value=Test user, match=false], Detail [name=Other Known Name, value=Jonathan, match=false], Detail [name=Reddit, value=/u/johns, match=true]], proofs=[], link=http://domain.gg/users?id=2384], Entity [id=1680, details=[Detail [name=Other Known Name, value=Johny, match=false], Detail [name=SteamID64, value=76561198213003675, match=true]], proofs=[], link=http://domain.gg/users?id=1680], Entity [id=1689, details=[Detail [name=Other Known Name, value=JohnnyPeto, match=false], Detail [name=SteamID64, value=76561198094228192, match=true]], proofs=[], link=http://domain.gg/users?id=1689]], notice=Showing 4 out of 4 matches.]]
Despite there are answers suggesting you to use Jackson, you can still accomplish easily with Gson with its default configuration just creating proper relations between mappings:
// A generic response, parameterized with <T>, can hold any type except of primitives
final class Response<T> {
#SerializedName("return")
final T ret = null;
}
final class EntitiesAndNotice {
final List<Entity> entities = null;
final String notice = null;
}
final class Entity {
// Unlike Object and any its subclasses, `int` being a primitive cannot be nulled
// Simple 0 won't work either, because the compiler will inline it
// So it's a sort of cheating javac to return a value that holds 0 already
final int id = Integer.valueOf(0);
final List<Detail> details = null;
// Your JSON document does not provide enough info on the elements type
// So it depends on how Gson parses JSON tokens
final List<Object> proofs = null;
final URL link = null;
}
final class Detail {
final String name = null;
final String value = null;
// The same for primitive booleans, or Boolean.FALSE
final boolean match = Boolean.valueOf(false);
}
Example use:
private static final String JSON = "{\"return\":{\"entities\":[{\"id\":2385,\"details\":[{\"name\":\"Other Known Name\",\"value\":\"John Wick\",\"match\":false}],\"proofs\":[],\"link\":\"http://domain.gg/users?id=2385\"},{\"id\":2384,\"details\":[{\"name\":\"Discord ID\",\"value\":\"159985870458322944\",\"match\":false},{\"name\":\"SteamID64\",\"value\":\"76561197991558078\",\"match\":true},{\"name\":\"SteamVanity\",\"value\":\"test\",\"match\":false},{\"name\":\"PS4\",\"value\":\"John_S\",\"match\":false},{\"name\":\"XBox\",\"value\":\"John S\",\"match\":false},{\"name\":\"Email\",\"value\":\"john_smith#gmail.com\",\"match\":true},{\"name\":\"Comment\",\"value\":\"Test user\",\"match\":false},{\"name\":\"Other Known Name\",\"value\":\"Jonathan\",\"match\":false},{\"name\":\"Reddit\",\"value\":\"/u/johns\",\"match\":true}],\"proofs\":[],\"link\":\"http://domain.gg/users?id=2384\"},{\"id\":1680,\"details\":[{\"name\":\"Other Known Name\",\"value\":\"Johny\",\"match\":false},{\"name\":\"SteamID64\",\"value\":\"76561198213003675\",\"match\":true}],\"proofs\":[],\"link\":\"http://domain.gg/users?id=1680\"},{\"id\":1689,\"details\":[{\"name\":\"Other Known Name\",\"value\":\"JohnnyPeto\",\"match\":false},{\"name\":\"SteamID64\",\"value\":\"76561198094228192\",\"match\":true}],\"proofs\":[],\"link\":\"http://domain.gg/users?id=1689\"}],\"notice\":\"Showing 4 out of 4 matches.\"}}";
private static final Gson gson = new Gson();
private static final TypeToken<Response<EntitiesAndNotice>> responseTypeToken = new TypeToken<Response<EntitiesAndNotice>>() {
};
public static void main(final String... args) {
final Response<EntitiesAndNotice> response = gson.fromJson(JSON, responseTypeToken.getType());
final String value = response.ret.entities.get(1).details.get(3).value;
System.out.println(value);
}
Output:
John_S

Categories