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
Related
I'm struggling with some performance of JSON mapping. I get JSON response with an array of objects like that:
[{"id": 38699739,
"kind": 75,
"quantity": 17796,
"quality": 0,
"price": 6.8,
"seller":
{"id": 1922384,
"company": "Bodland co",
"realmId": 0,
"logo": ""},
"posted": "2022-08-06T13:11:58.243914+00:00",
"fees": 3631},
...200+ more...]
What I want to get from it is collection of custom class containing "kind", "quality" and "price". I want filter the response to get me only one object with the lowest "price" of each "quality". For now I do mapping of whole JSON which takes relatively lot of time and memory since I got 200+ JSONs like that with even more objects in it, and then do some filtering.
Is this doable? If not by jsonpath maybe some of you will propose different/better approach to that problem.
Many thanks.
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.
I need to process a big JSON payload(~1MB) coming from an API, a portion of the JSON is something like this:
{
"id": "013dd2a7-fec4-4cc5-b819-f3cf16a1f820",
//more attributes
"entry_mode": "LDE",
"periods": [
{
"type": "quarter",
"id": "fe96dc03-660c-423c-84cc-e6ae535edd2d",
"number": 1,
"sequence": 1,
"scoring": {
//more attribtues
},
"events": [
{
"id": "e4426708-fadc-4cae-9adc-b7f170f5d607",
"clock": "12:00",
"updated": "2013-12-22T03:41:40+00:00",
"description": "J.J. Hickson vs. DeAndre Jordan (Blake Griffin gains possession)",
"event_type": "opentip",
"attribution": {
"name": "Clippers",
"market": "Los Angeles",
"id": "583ecdfb-fb46-11e1-82cb-f4ce4684ea4c",
"team_basket": "left"
},
"location": {
"coord_x": 572,
"coord_y": 296
},
"possession": {
"name": "Clippers",
"market": "Los Angeles",
"id": "583ecdfb-fb46-11e1-82cb-f4ce4684ea4c"
}
},
//more events
]
}
]
}
This is a nearly-realtime API that I need to process only the events, identify a set of event UUIDs, look for duplicates in the database and save new events.
I could use a JSONObject/JSONArray or use regex with string parsing to and fetch the events portion. Processing time is critical since this should be nearly-realtime and memory efficiency is important since there can be multiple payloads coming in at once.
Which one is more efficient for this use case?
Use a proper streaming JSON parser. You know what you want to pull out of the stream, you know when you can quit parsing it, so read the stream in small, manageable chunks, and quit as soon as you know you are done.
Circa 2017, I'm not aware of any browser/native JSON streaming APIs, so you'll need to find a Javascript-based streaming library. Fortunately, streaming is not a new concept, so there are a number of options already in existence:
http://oboejs.com/
https://github.com/dominictarr/JSONStream
https://github.com/creationix/jsonparse
https://github.com/dscape/clarinet
http://danieltao.com/lazy.js/demos/json/
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!
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.