Spring Response to Java Object when Json KEY keeps changing - java

I am calling an external rest-API from my spring boot code which returns JSON something like this.
{
"Jack": {
"employeeId": 1,
"active": 1,
"hours": 40
},
"Ryan": {
"employeeId": 2,
"active": 1,
"hours": 40
},
"Ken": {
"employeeId": 3,
"active": 1,
"hours": 40
}
}
I am trying to convert this to java pojo using jackson and using RestTemplate to call this
I am not sure how to create java class for something like this, as the employee name keeps changing and there is No "Name key" in JSON. I can create a class for the fields employeeid,active and hours but now sure how to accommodate the employee name
Is there a way to convert these to java objects

The best way is to change the API response like "name":"Jack".It seems that it is not a good json form, isn't it?
But if you can do this,maybe this question will not exists.
The other way to convert is to convert is as Map<String,Object> and then you can get key set and you can handle the object as usual json.

Related

How to do you convert POJO to JSON and vice-versa using the Java Mongo API only (without storing first)

My mobile app receives a JSON document from my server, but then my app sends it back to the server with additional fields populated, some fields modified.
Upon receipt at the server I can convert this JSON string to a generic Mongo Document and dump it into the database, I can then read it back as a POJO. This is wasteful, all I want to do is convert the JSON to a POJO using the mongo API, manipulate the data a little, then store it.
Likewise I sometimes don't want to query as the generic Document but a specific typed POJO then create the Document / JSON from it after manipulating it in a type-safe way.
Ideally I'd like to support parsing a list of documents too. Using the Java Mongo API is it possible to
Convert a POJO to a JSON String
Convert a JSON string (or Document) to POJO
Convert a JSON array to a List
Convert a List to a JSON List of documents
Please note that I'm trying to avoid using JAX-RS / JAXB or Spring, or other third party libraries such as GSON as so far they don't keep the JSON in the same format as if I queried the mongo database using the generic Document type.
As a really simple example, my JSON has the _id and date properties like this, some entities will have nested child documents/types ...
{
"questions": [
{
"_id": {
"$oid": "605b3d62958763a2e18be480"
},
"companyId": {
"$oid": "554a0123c2e625bd02259318"
},
"groupId": null,
"title": "Test Title 1",
"desc": "Description 1",
"style": "range",
"minRange": 1,
"maxRange": 10,
"comments": false,
"startDate": {
"$date": "2021-03-01T00:00:00Z"
},
"endDate": {
"$date": "2021-03-31T23:59:00Z"
},
"deleted": true,
"frequencyMins": 1
},
..
],
}
}

Rest assured and Java: how to get wanted JSON object/body?

Lets assume I have a GET request that returns something like the following:
[
{
"id": 1,
"name": "Mark"
},
{
"id": 2,
"name": "Steve"
},
{
"id": 3,
"name": "Bill"
}
]
How can I return the wanted object from a List? or something that contains maybe this JSON as a String or what is the correct approach to get only one of the items from the response for example lets say i need to get Bills info only so i want to parse that JSON to get only this:
{
"id": 3,
"name": "Bill"
}
And no, I do not want to do this parsing in the GET request URL. I need to be able to get it from the list of everything that the GET request returns.
"$[2]"
In JsonPath, $ represents the anonymous root of the queried JSON, for cases like this one when you need to refer to it directly instead of stuff like "stuff.things[8]"
In this case, the array you're trying to analyze is the anonymous root, so you refer to it as $. Then you want the element at index 2 of this array, so it's $[2]
I did another solution that actually fits exactly for my needs. I deserialized the JSON response into a List of POJO classes that the JSON body corresponds like this:
List<MyEntity> myList = new ArrayList<>();
myList = given().
contentType(ContentType.JSON).
when().
get(getURL).
then().
extract().
body().
jsonPath().
getList(".", MyEntity.class);
This way I get a List of the initialized MyEntity classes and I can just do anything I need to, for example I just modify the values like this:
myList.get(0).setName("newName");
Then I can just send them back with a POST/PUT calls or something like that. Works like a charm!

The field in returned json is an id referenceing to other json, how to parse it

Hi guys I encountered a problem on handling json on Android today. The json I got from the web service looks like the below:
e.g. from https://serviice.somewebite.com/list.json, I got this :
{
"task_id": 5,
"profile_id": 3,
"text": "{profileName} wrote {taskName}",
"created_at": "2015-08-10",
"event": "post"
},
{
"task_id": 6,
"profile_id": 2,
"text": "{profileName} wrote {taskName}",
"created_at": "2015-10-24",
"event": "post"
},
...... (and many similar entities)
the task_id is actually an id linking to other remote json, for the first entity, task_id = 5, which I should get the task json from https://serviice.somewebite.com/task/5.json, like the below:
{
"id": 5,
"name": "Clean house",
"description": "Clean every room in the house",
"state": "assigned",
}
And it is the similar way to get the profile json in the list.json file.
I have been handling json with pojo + retrofit + GSON before, but all the content are in one json, not like this.
What I want is getting all the needed jsons and show them in a listview including created_at(in list.json), task_description(in task.json), task_name(in task.json)
Currently I can only figure out one way to hanlde it. First get the list.json , and then scan the task ids and profile ids in list.json, and then get all the needed json via multiple asynchronous http requests. Seems too much boiler plate code here.
Is there an existing library/framework for this scenario? I am on Android platform using Java. Thanks a lot!

Convert nested arbitrary JSON to CSV in Java

This question has been asked many times but I couldn't find the answer that fixes my issue.
I'm trying to convert nested JSON format to CSV format like this :
The JSON structure is arbitrary and could be anything, nested or not.
I'm not suppose to know it, it's a database answer and I need to export this JSON answer into CSV file.
Here is an example
Input :
{
"_id": 1,
"name": "Aurelia Menendez",
"scores": [
{
"type": "exam",
"score": 60.06045071030959
},
{
"type": "quiz",
"score": 52.79790691903873
},
{
"type": "homework",
"score": 71.76133439165544
}
]
}
The output I'm looking for :
_id,name,scores.type,scores.score,scores.type,scores.score,scores.type,scores.score
1,Aurelia Menendez,exam,60.06...,quiz,52.79...,homework,71.76..
This is an example, it could be any other JSON document.
The idea here is to use dot notation in the CSV column name.
I've already used CDL but the output is not what I want :
_id scores name
1 "[{score:60.06045071030959,type:exam},{score:52.79790691903873,type:quiz},{score:71.76133439165544,type:homework}]" Aurelia Menendez
So how can I convert nested JSON to CSV with dot notation and in a generic way ?
Edits
Deserialisation of the JSON with Jackson :
ObjectMapper mapper=new ObjectMapper();
JsonNode jsonNode=mapper.readValue(new File("C:\\...\\...\...\\test.json"), JsonNode.class);
Ismail
Like you said :
The JSON structure is arbitrary and could be anything, nested or not.
The JSON to CSV conversion can't be generalized as it varies from user to user and also depends specific requirements.
But still there's a library json2flat which tries to achieve it. But it may differ from user's requirement. Still it's worth a try.
For example for the JSON given above:
{
"_id": 1,
"name": "Aurelia Menendez",
"scores": [
{
"type": "exam",
"score": 60.06045071030959
},
{
"type": "quiz",
"score": 52.79790691903873
},
{
"type": "homework",
"score": 71.76133439165544
}
]
}
can be interpreted as follows :
/_id,/name,/scores/type,/scores/score
1,"Aurelia Menendez","exam",60.06045071030959
1,"Aurelia Menendez","quiz",52.79790691903873
1,"Aurelia Menendez","homework",71.76133439165544
Converting JSON to XLS/CSV in Java has what you are looking for.
Basically, you need to use org.json.CDL to convert from JSON to CSV format
Comments are not convenient place to post longs answers, so I post my answer here.
Analyze your JSON and all possible JSON structures you can get from your database. It should be a limited number of JSON forms.
As you have analyzed your JSON structure build a class/class hierarchy, that fully reflects this structure.
Use JSON serializer/deserializer library at your choice, to deserialize JSON to a java object.
Employ StringBuffer/StringBuilder classes, and iterate over your object information, and build comma delimited (or tab-delimited) strings.
Write strings you have built on the previous stage to the file.
That's it.

Format json file proberly and parse it to java

For an Android App I'm working on I need to parse json files with various informations to my App, for now the files look like this:
{
"R6":{
"Typ": "KnotenRaum",
"ID": 1,
"X-Koor": 3,
"Y-Koor": 11,
"Ebene": 0,
"Kantenliste": [ "m7" ],
"GruppenID": 1,
"Raum": {
"Nummer": "A.00.01",
"Typ": null,
"Person": null
}
},
"H107":{
"Typ": "KnotenTreppe",
"ID": 115,
"X-Koor": 7,
"Y-Koor": 3,
"Ebene": 1,
"Kantenliste": [ "h108","b1002" ],
"GruppenID": 1,
"Raum": {}
}
}
As this is my (or to be more specific our) first time using json I'm not sure if this is a "good" way of formating the file. The problem is, that I don't only have 5 or 10 objects but i guess hundreds, so i think using specifiers like "H107" is the wrong way and I should just put it all into one large array?
And second: if I put it in such an array, what is the best way to parse it to java?
I don't need class objects (at least not necessarily) as all those data will be stored in a sqlite database when the app runs for the first time.
I found some examples but mostly those only consist of one object and are a lot simpler than my resulting json file I guess.
H107 and R6 are object names?
I think this can be a array with multiple objects where "R6" can be a JSON key on object.
for example:
[
{
"name": "R6",
"Typ": "KnotenRaum",
"ID": 1,
"X-Koor": 3,
"Y-Koor": 11,
"Ebene": 0,
"Kantenliste": [
"m7"
],
"GruppenID": 1,
"Raum": {
"Nummer": "A.00.01",
"Typ": null,
"Person": null
}
},
{
"name": "H107",
"Typ": "KnotenTreppe",
"ID": 115,
"X-Koor": 7,
"Y-Koor": 3,
"Ebene": 1,
"Kantenliste": [
"h108",
"b1002"
],
"GruppenID": 1,
"Raum": {}
}
]
About objects I recommend you use an object to represent this JSON, is more readable and more easy to save on SQLite, you can use a framework like Jackson or GSON to parse this JSON automatically into objects.
I think it would be wise to store the data into Objects.
It's easier to insert objects into SQLdatabase as well as making a JSON objects array.
Andhere is a nice tutorial on the subject:
Android JSON tutorial

Categories