Create a json from a List of list Java - java

I have a list of lists that I want to convert to json, the list looks like:
[Country, Food, Detail, Population, Region, Extension]
[Germany, 1, 2, 3, 4, 5]
[England, 10, 11, 12, 13, 14]
[USA, 19, 20, 21, 22, 23]
[China, 28, 29, 30, 31, 32]
[Italy, 37, 38, 39, 40, 41]
[France, 46, 47, 48, 49, 50]
I want to create a json like this
"Germany"{
"Food":
{
"value": 1
},
"Detail":
{
"value": 2
},
"Population":
{
"value": 3
},
"Region":
{
"value": 4
},
"Extension":
{
"value": 5
}
}
"Italy"{
"Food":
{
"value": 10
},
"Detail":
{
"value": 11
},
"Population":
{
"value": 12
},
"Region":
{
"value": 13
},
"Extension":
{
"value": 14
}
}...
I was trying with gson but I have can not created it, I have a problem when I add the other json into the last json. I get a result like this:
"Germany"{
"Food":
{
"value": 1
}
...}
"Germany"{
"Food":
{
"value": 10
}
"Detail":
{
"value": 11
}
...}
"Italy"{
"Food":
{
"value": 10
}
"Detail": {
"value": 11
}
...}
This is the part of my code where I create the json
JsonObject main2= new JsonObject();
JsonObject main = new JsonObject();
for (int row = 1; row < table.size(); row++) {
for (int colum = 1; colum < table.get(0).size(); colum++) {
JsonObject jsObj = new JsonObject();
jsObj.add("value", new JsonParser().parse(table.get(row).get(colum)));
main.add(table.get(0).get(colum), jsObj);
}
main2.add(table.get(row).get(0),main);
gson.toJson(main2, writer);
}
I am not sure what are I doing wrong, If I write the "main" the result is correct but when I write the "main2" I have that problem. There are other option because I tried in deferents ways buy I don have a correct result :(

In the last line of inner for loop, it should be main2 instead of main.

Related

JSONArray throws A JSONArray text must start with '[' at 1 [character 2 line 1] exception

The following JSON Object (a variable named json_object) is composed of two values
peripheral_devices
peripheral_tests
both of which are arrays:
{
"peripheral_devices": [
{
"_id": 1,
"active": -1,
"battery": "63",
"bt_firmware_version": "btv1.7.777",
"configured": 0,
"connected": 0,
"consumer_id": 22,
"create_date": 1635807323224,
"device_id": 72,
"discovered": 0,
"firmware_version": "v3.14",
"hardware_version": "null",
"icon": "icon_device_bp5",
"last_connect_date": 1640023710420,
"mac_address": "8C:DE:52:41:FC:57",
"model": "BP5 41FC57",
"name": "BP5",
"other_id": "-1",
"paired": -1,
"type_id": 1,
"update_date": 1635807323224,
"user_id": 13
},
{
"_id": 3,
"active": -1,
"battery": "90",
"bt_firmware_version": "1.0.0",
"configured": -1,
"connected": 0,
"consumer_id": 22,
"create_date": 1635807323239,
"device_id": 72,
"discovered": 0,
"firmware_version": "1.0.0",
"hardware_version": "5.0.0",
"icon": "icon_device_hs2s",
"last_connect_date": 1640022147928,
"mac_address": "00:4D:32:0C:B7:2C",
"model": "HS2S 11070",
"name": "HS2S",
"other_id": "-1",
"paired": -1,
"type_id": 3,
"update_date": 1635807323239,
"user_id": 13
}
],
"peripheral_tests": [
{
"_id": 199,
"consumer_id": 22,
"create_date": 1640020949760,
"end_date": 1640020949078,
"mood_id": -1,
"notes": "",
"start_date": 1640020939533,
"status_id": 1,
"type_id": 2,
"update_date": 0,
"user_id": -99
},
{
"_id": 198,
"consumer_id": 22,
"create_date": 1640020904183,
"end_date": -1,
"metric_bps": {,
"mood_id": -1,
"notes": "",
"start_date": 1640020863742,
"status_id": 1,
"type_id": 1,
"update_date": 0,
"user_id": -99
},
{
"_id": 197,
"consumer_id": 22,
"create_date": 1640020834664,
"end_date": 1640020828741,
"mood_id": -1,
"notes": "",
"start_date": 1640020822580,
"status_id": 1,
"type_id": 3,
"update_date": 0,
"user_id": -99
}
]
}
and derived as follows:
JSONObject json_object = new JSONObject(post_data);
Using org.json.JSONArray.JSONArray:
JSONArray peripheral_devices = json_object.getJSONArray("peripheral_devices");
works as expected and a json array is parsed from json_object. However, using the same technique to get the second array in the object does not:
JSONArray json_tests = new JSONArray("peripheral_tests");
throws exception:
A JSONArray text must start with '[' at 1 [character 2 line 1]
Why? What am I not understanding?
You code
JSONArray json_tests = new JSONArray("peripheral_tests");
Take string in the constructor which is the JSON data and peripheral_tests is not JSON array, to get peripheral_tests you need to get it from json_object like
JSONArray peripheral_devices = json_object.getJSONArray("peripheral_tests");
The input JSON is invalid, accoring to JSON Lint ...which may lead to unexpected results:
Error: Parse error on line 69:
..."metric_bps": { , "mood_id": -1,
----------------------^
Expecting 'STRING', '}', got ','

How to get a specific value from json request in Java [duplicate]

This question already has answers here:
How to parse JSON in Java
(36 answers)
Closed 3 years ago.
When I do this in git bash:
curl -XPOST "https://evepraisal.com/appraisal.json?market=jita&raw_textarea=tritanium&persist=no"
I will get a JSON response like this:
{
"appraisal": {
"created": 1582333288,
"kind":" listing",
"market_name": "jita",
"totals": {
"buy": 13.96,
"sell": 14.62,
"volume": 0.02
},
"items":[
{
"name": "tritanium",
"typeID": 34,
"typeName": "Tritanium",
"typeVolume": 0.01,
"quantity": 2,
"prices": {
"all": {
"avg": 7.792728706899779,
"max": 6660,
"median": 8.49,
"min": 0.01,
"percentile": 10,
"stddev": 11.835529435279334,
"volume": 22008301773,
"order_count": 78
},
"buy": {
"avg": 4.520566796010778,
"max": 6.98,
"median": 5,
"min": 0.01,
"percentile": 6.98,
"stddev": 1.2552914981531016,
"volume": 3832347488,
"order_count": 33
},
"sell": {
"avg": 8.740860647577046,
"max": 6660,
"median": 8.49,
"min": 7.31,
"percentile": 7.31,
"stddev": 12.927105236235532,
"volume": 18175954285,
"order_count": 45
},
"updated": "2020-02-22T00:58:22.615370433Z",
"strategy":" orders"
},
"meta": {
}
}
],
"raw": "tritanium x 2",
"parser_lines": {
"listing": [
0
]
},
"unparsed": {
},
"private": false,
"price_percentage": 100,
"live": false,
"expire_minutes": 21600
}
}
My question is:
How can I pick up a specific attribute from this JSON response?
Like totals:sell:14.62
My second question is:
How can I do that in Java and put the specific attribute in a variable?
Everything depends on what do you exactly need.
If just get one value from response just take it from the response with JSONObject.
you could create Java's POJO for your response, like ResponseWrapper class. Deserialise the response to Java model. Then you can call something like:
double sell = responseObject.getTotals().getSell();
Additional info:
Parse JSON in Java

How to get data from an Old DB to a new One with different coulmns settings?

I'm using Spring Boot, MYSQL and Angular 7.
I have to redo the social network of my company taking data from their old DB.
I improved it changing some columns settings but I have to get every data for each user.
OLD DB JSON EXAMPLE:
{
***PROBLEM, have no idea why they do the first row like this***
"e92b088c-5546-47bc-a9cf-2dc6da22c594": {
"uuid": "e92b088c-5546-47bc-a9cf-2dc6da22c594",
"skills": [
{
"id": 64,
"name": "C#",
"certified": false
},
{
"id": 66,
"name": "ASP.NET",
"certified": false
},
{
"id": 73,
"name": "Sharepoint",
"certified": false
},
],
}
}
NEW DB JSON must look like this:
{
"uuid": "e92b088c-5546-47bc-a9cf-2dc6da22c594",
"skills": [
{
"id": 64,
"name": "C#",
"certified": false
},
{
"id": 66,
"name": "ASP.NET",
"certified": false
},
{
"id": 73,
"name": "Sharepoint",
"certified": false
},
]
}
I have 16k rows and I need to associate the first row of the old JSON with my uuid in new JSON.
I don't know why they did the first row like that and I have no idea how to solve it.
Is it possible to do it in Java?

Grouping by ID in mongoDB

Can anyone help me with the following aggregate operation in mongodb: having a collection of items with ids and group ids, group them by group ids. For example, for collection of items:
{
"id": 1,
"group_id": 10,
"data": "some_data",
"name": "first"
},
{
"id": 2,
"group_id": 10,
"data": "some_data",
"name": "second"
},
{
"id": 3
"group_id": 20,
"data": "some_data",
"name": "third"
}
Create new collection of groups with the following structure:
{
"id": 10,
"items": [
{
"id": 1,
"group_id": 10,
"data": "some_data",
"name": "first"
},
{
"id": 2,
"group_id": 10,
"data": "some_data",
"name": "second"
}
]
},
{
"id": 10,
"items": [
{
"id": 2,
"group_id": 20,
"data": "some_data",
"name": "third"
}
]
}
The corresponding snippet with Java and spring-data-mongodb will also be appreciated.
In fact I'm doing the same right now with Java and want to move this logic to mongo for paging optimisation.
You can do it with the folowwing simple group aggregation:
db.table.aggregate(
[
{
$group: {
_id : "$group_id",
items : { "$push" : "$$ROOT" }
}
}
]
);
When you want to output the data from the aggregation into a new collection use the $out operator

Java - returning JSON from servlet with Gson library

I am using GSON library.
I have a programm which returns JSON.
JSON constructs and returns in this way:
Gson gson = new Gson();
//findNewComments returns List<Comment> comments
return gson.toJson(service.findNewComments(id,lastId));
So the result is:
[
{
"id": 43,
"entryId": 19,
"author": " m8w46",
"body": "mw86",
"date": "WED 9, 2011"
},
{
"id": 44,
"entryId": 19,
"author": " n7w4",
"body": "nw77w4",
"date": "WED 9, 2011"
}
]
But this array must be named as "comments"!
"comments": [
{
"id": 43,
"entryId": 19,
"author": " m8w46",
"body": "mw86",
"date": "WED 9, 2011"
},
{
"id": 44,
"entryId": 19,
"author": " n7w4",
"body": "nw77w4",
"date": "WED 9, 2011"
}
]
How can i do that?
Not sure if this is acceptable to you but:
public class CommentWrapper {
List<Comments> comments;
public CommentWrapper(List<Comment> comments) {
this.comments = comments;
}
}
Then you can do:
return new Gson().toJson(new CommentWrapper(service.findNewComments(id,lastId)));
Which results in:
{
"comments": [
....your data here...
]
}
Not sure if the object syntax is acceptable to you or not.
Aside from wrapper object, you can also use a simple java.util.Map with single entry, possibly bit less code.

Categories