Getting counts of arrays in a JsonNode - java

I have a json structure like this:
{
"MetricDataResults": [
{
"Id": "m1",
"StatusCode": "Complete",
"Label": "AWS/EC2 NetworkOut Average",
"Timestamps": [
1518868032,
1518867732,
1518867432
],
"Values": [
15000,
14000,
16000
]
},
{
"Id": "m2",
"StatusCode": "Complete",
"Label": "AWS/EC2 NetworkOut SampleCount",
"Timestamps": [
1518868032,
1518867732,
1518867432
],
"Values": [
15,
14,
16
]
},
{
"Id": "m3",
"StatusCode": "Complete",
"Label": "AWS/EC2 HealthyHostCount",
"Timestamps": [
1518868032,
1518867732,
1518867432
],
"Values": [
15,
14,
16
]
}
]
}
For this json structure I need the counts of arrays in it. I'm looking at the structure to look like something like this:
{
"MetricDataResults (3)": [
{
"Id": "m1",
"StatusCode": "Complete",
"Label": "AWS/EC2 NetworkOut Average",
"Timestamps (3)": [
1518868032,
1518867732,
1518867432
],
"Values (3)": [
15000,
14000,
16000
]
},
{
"Id": "m2",
"StatusCode": "Complete",
"Label": "AWS/EC2 NetworkOut SampleCount",
"Timestamps (3)": [
1518868032,
1518867732,
1518867432
],
"Values (3)": [
15,
14,
16
]
},
{
"Id": "m3",
"StatusCode": "Complete",
"Label": "AWS/EC2 HealthyHostCount",
"Timestamps (4)": [
1518868032,
1518867732,
1518867432,
1518867462,
],
"Values (4)": [
15,
14,
16,
18
]
}
]
}
I'm wondering if there's a particular way with which we get the counts when deserializing with jackson? I was looking at this post - How to get count all json nodes using Jackson framework
but that doesn't seem to completely align with the use case I have. Does anybody have an idea on whether this can be done?

Related

How to get json key values by another key value

I have a JSON output like this:
{
"items": [
{
"id": "1",
"name": "Anna",
"values": [
{
"code": "Latin",
"grade": 1
},
{
"code": "Maths",
"grade": 5
}
]
},
{
"id": "2",
"name": "Mark",
"values": [
{
"code": "Latin",
"grade": 5
},
{
"code": "Maths",
"grade": 5
}
]
}
]
}
I need to get field values for "name": "Anna". I am getting RestAssured Response and would like to use my beans to do that, but I can also use jsonPath() or jsonObject(), but I don't know how. I searched many topics but did not find anything.

How to Iterate through Json schema and values based on keys

I have this JSON schema. I want to read it line by line and populate all "name" key values to an ArrayList of String until it reaches a key called entity. When it reaches to an entity I want to read the entity value and populate it to a String.After that loop should go through the remains of the schema and read all the "name" and populate to a new ArrayList until it meets another entity and so on...
I've only included a part of the schema it's a valid schema.
{
"entity": "Data",
"xement": [
{
"code": "MA",
"entity": "MH",
"attr": [
{
"name": "Id",
"position": 1
},
{
"name": "mdc",
"position": 4
},
{
"name": "minr",
"position": 10
},
{
"name": "mx",
"position": 11
}
],
"maltr": [
{
"name": "sub",
"position": 13
}
]
},
{
"code": "war",
"entity": "subl",
"attr": [
{
"name": "se",
"position": 1
},
{
"name": "go",
"position": 2
},
{
"name": "re",
"position": 4
},
{
"name": "pr",
"position": 10
},
{
"name": "mxp",
"position": 11
},
{
"name": "rpl",
"position": 45
},
{
"name": "rtr",
"position": 47
},
{
"name": "net",
"position": 55
}
],
"groups": [
{
"entity": "ro",
"grattr": [
{
"name": "rmn",
"position": 5
},
{
"name": "aib",
"position": 6
},
{
"name": "nxr",
"position": 7
},
{
"name": "xer",
"position": 8
},
{
"name": "rog",
"position": 9
},
{
"name": "ccc",
"position": 16
}
]
}
]
}
]
}
What is the best way to do that?

compare and combine two Objects in java which produces as JSON

I have two objects of type ClassA<List<List<String>>> which retruns as a JSON from controller. I want to merge these two objects based on two properties(country and operator) if they are equal. Example JSON looks like
#RequestMapping(value = "/controller3"method= RequestMethod.GET produces= MediaType.APPLIACTION_JSON_VALUE)
public ClassA<List<List<String>>> process(HttpServletRequest request) {
ClassA<List<List<String>>> one = service.getvalues();
ClassA<List<List<String>>> two = service.getvaluestwo();
return merge(one, two);
}
JSON1 looks like
"title": "Source",
"dateRange": "23 August 2017 - 21 November 2017",
"total": 336,
"pageSize": 336,
"pageNum": -1,
"nextPageKey": "",
"results": [
[
"India",
"Idea",
"30"
],
"headers": [
"Country",
"Operator",
"Count"
]
JSON2 looks like
"title": "Source",
"dateRange": "23 August 2017 - 21 November 2017",
"total": 336,
"pageSize": 336,
"pageNum": -1,
"nextPageKey": "",
"results": [
[
"India",
"Idea",
"20"
],
"headers": [
"Country",
"Operator",
"Count"
]
I want the Resultant JSON would extracted from RestController from the type ClassA<List<List<String>>> would looks like
"title": "Source",
"dateRange": "23 August 2017 - 21 November 2017",
"total": 336,
"pageSize": 336,
"pageNum": -1,
"nextPageKey": "",
"results": [
[
"India",
"Idea",
"30",
"20",
"50"
],
"headers": [
"Country",
"Operator",
"Count1",
"Count2",
"Total"
]
if there is no match then aggregate them as with zero count of each other.
Can you guys help me?

Having trouble accessing fields of JSON using groovy

I'm trying to extract the information from an API response using groovy
I want to get to the info stored under the "res" key.
Something along the lines of body.measures.02:00:00:02:06:70.res
Can not figure out have to access this information without throwing a nullpointerexception.
"body":[ {
"_id":"70:ee:50:01:fe:96",
"place":{
"location":[
-70.863189,
42.273936
],
"altitude":26.154942,
"timezone":"America\/New_York"
},
"mark":12,
"measures":{
"02:00:00:02:06:70":{
"res":{
"1506611038":[
22,
66
]
},
"type":[
"temperature",
"humidity"
]
},
"06:00:00:01:97:28":{
"wind_strength":15,
"wind_angle":343,
"gust_strength":29,
"gust_angle":301,
"wind_timeutc":1506611083
},
"70:ee:50:01:fe:96":{
"res":{
"1506611086":[
1007.4
]
},
"type":[
"pressure"
]
}
},
"modules":[
"02:00:00:02:06:70",
"06:00:00:01:97:28"
],
"module_types":{
"02:00:00:02:06:70":"NAModule1",
"06:00:00:01:97:28":"NAModule2"
}
}
],
"status":"ok",
"time_exec":0.017483949661255,
"time_server":1506611446
}
body is an array with one element in your case, so you have to do body[0].measures.'02:00:00:02:06:70'.res, e. g. like
new groovy.json.JsonSlurper().parseText('''{"body":[ {
"_id":"70:ee:50:01:fe:96",
"place":{
"location":[
-70.863189,
42.273936
],
"altitude":26.154942,
"timezone":"America/New_York"
},
"mark":12,
"measures":{
"02:00:00:02:06:70":{
"res":{
"1506611038":[
22,
66
]
},
"type":[
"temperature",
"humidity"
]
},
"06:00:00:01:97:28":{
"wind_strength":15,
"wind_angle":343,
"gust_strength":29,
"gust_angle":301,
"wind_timeutc":1506611083
},
"70:ee:50:01:fe:96":{
"res":{
"1506611086":[
1007.4
]
},
"type":[
"pressure"
]
}
},
"modules":[
"02:00:00:02:06:70",
"06:00:00:01:97:28"
],
"module_types":{
"02:00:00:02:06:70":"NAModule1",
"06:00:00:01:97:28":"NAModule2"
}
}
],
"status":"ok",
"time_exec":0.017483949661255,
"time_server":1506611446
}}''').body[0].measures.'02:00:00:02:06:70'.res

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

Categories