I have a json Structure like this
[{
"startAt": 1617605301292,
"endAt": 1617605317095,
"duration": 15803,
"selection": {
"selected.Speed": "0",
"selected.Low": "65535",
"selected.Fast": "7173",
"selected.medium": "5"
},
"details": {
"phase": [{
"value": "2",
"timestamp": 1617605301316
}]
}
},....]
I need the count of every item inside selection object.
For Example
- 0 occurred 4 times
- 65535 occurred 2 times
- 7173 occurred 3 times
- 5 occurred 1 times
this is what I have done so far
Map<String, Long> counted = jObj.stream()
.filter(y -> ((Map<String, Object>) y.get("selection")).containsKey("selected.Speed"))
.map(x -> {
String Speed = String.valueOf(((Map<String, Object>) x.get("details"))
.get("selected.Speed"));
return Speed;
}).collect(Collectors.toList()).stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
so from this I am able to get the count of a single key but not the every key inside json Object. Pardon if this is a very basic question, I am new to the stream.
TIA
x.get("details")which should bex.get("selection"). Is that the actual JSON or is there any intermediate processing step? And are you looking for values i.e. 3 times, 1 time... mapped to each property key? (i.e. 0 for selected.Speed occurred 4 times)0 selected.Speed occurred 4 times 65535 selected.Low occurred 2 time etcactually based on that I need to prepare a json which will be like this[{ "selected.Low ":65535, "count":2 },{ "selected.Low ":0, "count":4 } ]