How to loop over an ArrayList which contains a map? - java

Currently I'm using rest-assured to hit an endpoint and in turn store any JSON data matches via an ArrayList and HashMap.
I can see that I'm receiving a response back, but how do I loop over an ArrayList when it contains an internal HashMap?
As you can see from the JSON data below, I'm expecting to output all values / matches stored within the ArrayList.
My Code:
public void apiTest() {
String position = "Attacker";
String role = "PLAYER";
Response response = given()
.spec(footballCompetitions_requestSpecification)
.when().get(EndPoint.TEAMS + EndPoint.SQUAD);
ArrayList<Map<String, ?>> allPlayers = response.path
("squad.findAll { it.role == '%s' }.findAll { it.position == '%s' }", position, role);
Example JSON Data:
{
"id": 66,
"area": {
"id": 2072,
"name": "England"
},
"activeCompetitions": [
{
"id": 2021,
"area": {
"id": 2072,
"name": "England"
},
"name": "Premier League",
"code": "PL",
"plan": "TIER_ONE",
"lastUpdated": "2019-01-03T23:39:45Z"
},
{
"id": 2001,
"area": {
"id": 2077,
"name": "Europe"
},
"name": "UEFA Champions League",
"code": "CL",
"plan": "TIER_ONE",
"lastUpdated": "2018-12-13T18:55:02Z"
}
],
"name": "Manchester United FC",
"shortName": "Man United",
"tla": "MNU",
"crestUrl": "http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg",
"address": "Sir Matt Busby Way Manchester M16 0RA",
"phone": "+44 (0161) 8688000",
"website": "http://www.manutd.com",
"email": "enquiries#manutd.co.uk",
"founded": 1878,
"clubColors": "Red / White",
"venue": "Old Trafford",
"squad": [
{
"id": 3188,
"name": "David De Gea",
"position": "Goalkeeper",
"dateOfBirth": "1990-11-07T00:00:00Z",
"countryOfBirth": "Spain",
"nationality": "Spain",
"shirtNumber": 1,
"role": "PLAYER"
},
{
"id": 3331,
"name": "Marcus Rashford",
"position": "Attacker",
"dateOfBirth": "1997-10-31T00:00:00Z",
"countryOfBirth": "England",
"nationality": "England",
"shirtNumber": 10,
"role": "PLAYER"
},
{
"id": 3372,
"name": "Anthony Martial",
"position": "Attacker",
"dateOfBirth": "1995-12-05T00:00:00Z",
"countryOfBirth": "France",
"nationality": "France",
"shirtNumber": 11,
"role": "PLAYER"
},
{
"id": 3662,
"name": "Romelu Lukaku",
"position": "Attacker",
"dateOfBirth": "1993-05-13T00:00:00Z",
"countryOfBirth": "Belgium",
"nationality": "Belgium",
"shirtNumber": 9,
"role": "PLAYER"
},

If you are using Java 8 or higher, then just iterate over the List first and over the Map in a nested iteration using lambda expressions:
public static void main(String[] args) {
List<Map<String, ?>> jsonList = new ArrayList<>();
Map<String, String> mapOne = new HashMap<String, String>();
mapOne.put("id", String.valueOf(66));
mapOne.put("area", "Some Area");
jsonList.add(mapOne);
jsonList.forEach(map -> {
map.forEach((key, value) -> {
System.out.println(key + ": " + value);
});
});
}
The thing you have to take care of is the String representation of value, since it is a Map<String, ?> in your code and a Map<String, String> in my example, what may lead to problems if you directly use the above example's System.out.println(key + ": " + value);.

First iterate over the list, then iterate over the Map:
for (Map<String, ?> map : allPlayers) {
for (String key : map.keySet()) {
Object player = map.get(key);
// ...
}
}

Related

group a list of map by value and merge as grouped lists by sorting

I would like to group a list of maps by value and insert(merge) as grouped lists in certain conditions. Group by a value "yr" and merge "tag" by the same "category" then sum up the "price" them together, then after order in descending order by "price"
List<Grocery> groceryList = new ArrayList<>();
Grocery grocery = new Grocery();
List<HahsMap<String, Object>> tagList = new ArrayList<>();
HashMap<String, Object> tagMap = new HashMap<>();
## feeding data example
tagMap.put("category", "A");
tagMap.put("name", "Apple");
tagMap.put("price", 10);
tagList.add(tagMap);
grocery.setYr(String yr);
grocery.setTag(List<Hashmap<String, Object>> tagList);
groceryList.add(grocery);
This is an example of a list of maps.
List<Grocery> groceryList =
[
{
"yr": "2021",
"tag": [
{
"Category": "A",
"Name": "Apple",
"Price": 10
}
]
},
{
"yr": "2021",
"tag": [
{
"Category": "A",
"Name": "Apple",
"Price": 10
}
]
},
{
"yr": "2021",
"tag": [
{
"Category": "B",
"Name": "Banana",
"Price": 5
}
]
},
{
"yr": "2020",
"tag": [
{
"Category": "A",
"Name": "Apple",
"Price": 10
}
]
},
{
"yr": "2020",
"tag": [
{
"Category": "B",
"Name": "Banana",
"Price": 30
}
]
},
{
"yr": "2020",
"tag": [
{
"Category": "C",
"Name": "Candy",
"Price": 10
}
]
},
{
"yr": "2020",
"tag": [
{
"Category": "C",
"Name": "Candy",
"Price": 30
}
]
},
{
"yr": "2020",
"tag": [
{
"Category": "A",
"Name": "Apple",
"Price": 10
}
]
}
]
This is the sample output I want to get
List<Grocery> result =
[
{
"yr": "2021",
"tag": [
{
"Category": "A",
"Name": "Apple",
"Price": 20
},
{
"Category": "B",
"Name": "Banada",
"Price": 5
}
]
},
{
"yr": "2020",
"tag": [
{
"Category": "C",
"Name": "Candy",
"Price": 40
},
{
"Category": "B",
"Name": "Banada",
"Price": 30
},
{
"Category": "A",
"Name": "Apple",
"Price": 20
}
]
}
]
I know this is tricky to combine all the condition into one, could anyone help me out??
It will be really appreciated if at least I could have some ideas what to do about this..
I tried to group them in a 'yr' first, and try to find the values to map out then sort them out with the same 'category' then sum up.. but i have no clue on sorting them and merge them together with summed up 'price'..
Thanks in advance!!
Please check If this is what you are looking for.
String inputJson = "[{\"yr\":\"2021\",\"tag\":[{\"Category\":\"A\",\"Name\":\"Apple\",\"Price\":10}]},{\"yr\":\"2021\",\"tag\":[{\"Category\":\"A\",\"Name\":\"Apple\",\"Price\":10}]},{\"yr\":\"2021\",\"tag\":[{\"Category\":\"B\",\"Name\":\"Banana\",\"Price\":5}]},{\"yr\":\"2020\",\"tag\":[{\"Category\":\"A\",\"Name\":\"Apple\",\"Price\":10}]},{\"yr\":\"2020\",\"tag\":[{\"Category\":\"B\",\"Name\":\"Banana\",\"Price\":30}]},{\"yr\":\"2020\",\"tag\":[{\"Category\":\"C\",\"Name\":\"Candy\",\"Price\":10}]},{\"yr\":\"2020\",\"tag\":[{\"Category\":\"C\",\"Name\":\"Candy\",\"Price\":30}]},{\"yr\":\"2020\",\"tag\":[{\"Category\":\"A\",\"Name\":\"Apple\",\"Price\":10}]}]";
ObjectMapper objectMapper = new ObjectMapper();
//convert json to input Grocery List using jackson object mapper
List<Grocery> groceries = objectMapper.readValue(inputJson, new TypeReference<List<Grocery>>() {
});
List<Grocery> output = new ArrayList<>();
//group all groceries by year
Map<String, List<Grocery>> groceriesByYear = groceries.stream().collect(Collectors.groupingBy(grocery -> grocery.getYr()));
groceriesByYear.keySet().forEach(year -> {
//for each year, get the grocery list
List<Grocery> groceryList = groceriesByYear.get(year);
//merge tags which have same category
Map<String, Tag> tagsPerCategory = groceryList.stream().flatMap(grocery -> grocery.getTag().stream()).collect(Collectors.toMap(Tag::getCategory, Function.identity(),
(tag1, tag2) -> {
//merge function, sum up the price of tag which has same category
tag1.setPrice(tag1.getPrice() + tag2.getPrice());
return tag1;
}));
//create new grocery object with required data
Grocery grocery = new Grocery();
grocery.setYr(year);
grocery.setTag(new ArrayList<>(tagsPerCategory.values()));
output.add(grocery);
});
//output list will have groceries grouped by year and tag with summed up price
Edit
You can do filter on tag stream to avoid null tag objects and add not null check if you think price can be null or you can define a method to provide default value if price is not already set like below
public static <T> T getValueOrDefault(T value, T defaultValue) {
return value != null ? value : defaultValue;
}
and your tagsPerCategory looks like below
Map<String, Tag> tagsPerCategory = groceryList.stream().flatMap(grocery -> grocery.getTag().stream()).filter(tag -> tag != null && tag.getPrice() != null)
.collect(Collectors.toMap(Tag::getCategory, Function.identity(),
(tag1, tag2) -> {
//sum up the price of tag which has same category
tag1.setPrice(getValueOrDefault(tag1.getPrice(), 0) + getValueOrDefault(tag2.getPrice(), 0));
return tag1;
}));

ArrayList put a field in each map of the list according to the value of another arrayList

I have an arraylist:
{
"response": [
{
"number": "229425941 ",
"CURRENCY": "EUR",
"account": "0026.5501.90.0490520505",
"branch": "0154",
"product": "geia",
"service": "0026.0002.62.0300162968",
"amount": 20000,
"expDate": "2019-09-20",
"bank": "0026",
"name": "A name "
},
{
"number": "229425941 ",
"CURRENCY": "EUR",
"account": "0026.5501.90.0490520505",
"branch": "0154",
"product": "geia",
"service": "0026.0002.62.0300162968",
"amount": 20000,
"expDate": "2019-09-20",
"bank": "0027",
"name": "A name "
},
]
}
and I have a second arraylist which contains details for each bank. The list is this:
"details": [
{
"bankCode": "010",
"bankDescription": "BANK",
},
{
"bankCode": "011",
"bankDescription": "NATIONAL",
},
{
"bankCode": "012",
"bankDescription": "ALPHA",
}
]
}
Now I want to loop through the first list and put the bankDescription field in each object of the list according to the bankCode and the bankDescription of the second list. I made a stream for the first arrayList:
firtsList.stream().forEach(a -> {
a.put();
});
My guess is that somehow to loop through the second list but I don't know an efficient way. My output must be something like this:
{
"response": [
{
"number": "229425941 ",
"CURRENCY": "EUR",
"account": "0026.5501.90.0490520505",
"branch": "0154",
"product": "geia",
"service": "0026.0002.62.0300162968",
"amount": 20000,
"expDate": "2019-09-20",
"bank": "010",
"bankDesc: "BANK"
"name": "A name "
},
{
"number": "229425941 ",
"CURRENCY": "EUR",
"account": "0026.5501.90.0490520505",
"branch": "0154",
"product": "geia",
"service": "0026.0002.62.0300162968",
"amount": 20000,
"expDate": "2019-09-20",
"bank": "011",
"bankDesc: "NATIONAL"
"name": "A name "
},
]
}
secondList.stream().filter(bic -> firstList.stream()
.anyMatch(c -> StringUtils.substring((String) c.get("bankDesc"), 1, 3)
.equals(bic.get("bankCode"))));
Have a model/dto to simplify the process. You can use objectMapper to map Json directly into POJO.
Assuming you disregard all advice and proceed with Json/Map, here's how you can proceed:
//Build a dictionary from the data
Map<String, String> bankCodeToDescriptionMap = secondList.stream().collect(Collectors.toMap(v -> v.get("bankCode"), v -> v.get("bankDescription")));
firstList.stream().forEach(a -> {
a.put("bankDescription", bankCodeToDescriptionMap.get(v.get("bankCode")));
});
Do not iterate over the list for each value. It will be too slow

How to parse a flattened Json?

I have a requirement to use flattened structure JSON, for example below hierarchical Json:
{
"employees": [
{
"employee1": {
"employeeId": 123,
"name": "ABC",
"type": "permanent",
"address": {
"street": "",
"city": "",
"zipcode": 123456
},
"phoneNumbers": [
123456,
987654
],
"designation": "Manager",
"properties": {
"age": "29 years",
"joiningDate": "17-may-2017",
"salary": "1000 USD"
}
}
},
{
"employee2": {
"employeeId": 123,
"name": "XYZ",
"type": "parttime",
"address": {
"street": "",
"city": "",
"zipcode": 345645
},
"phoneNumbers": [
345332,
675444
],
"designation": "Contractor",
"properties": {
"age": "35 years",
"joiningatDate": "17-june-2015",
"salary": "700 USD"
}
}
}
]
}
**Could be represented as flat structure Json as below(generated this using json-flattener):**
{
"employees[0].employee1.address.zipcode": 123456,
"employees[0].employee1.address.city": "",
"employees[0].employee1.address.street": "",
"employees[0].employee1.name": "ABC",
"employees[0].employee1.employeeId": 123,
"employees[0].employee1.designation": "Manager",
"employees[0].employee1.type": "permanent",
"employees[0].employee1.phoneNumbers[0]": 123456,
"employees[0].employee1.phoneNumbers[1]": 987654,
"employees[0].employee1.properties.joiningDate": "17-may-2017",
"employees[0].employee1.properties.salary": "1000 USD",
"employees[0].employee1.properties.age": "29 years",
"employees[1].employee2.address.zipcode": 345645,
"employees[1].employee2.address.city": "",
"employees[1].employee2.address.street": "",
"employees[1].employee2.name": "XYZ",
"employees[1].employee2.employeeId": 123,
"employees[1].employee2.designation": "Contractor",
"employees[1].employee2.type": "parttime",
"employees[1].employee2.phoneNumbers[0]": 345332,
"employees[1].employee2.phoneNumbers[1]": 675444,
"employees[1].employee2.properties.joiningDate": "17-june-2015",
"employees[1].employee2.properties.salary": "700 USD",
"employees[1].employee2.properties.age": "35 years"
}
My problem is if my service receives flattened Json like above, how to convert it to java domain objects automatically like:
class Employee{
Address address;
Properties property;
}
Does any Java Json parser supports this automatic conversion or i will have to implement own parsing logic splitting the keys based on dot in the keys?
Thanks in Advance.
You can take a look at json-flattener.
It does what you want, and even more.
The usage is simple as this:
String json = "{ \"a\" : { \"b\" : 1, \"c\": null, \"d\": [false, true] }, \"e\": \"f\", \"g\":2.3 }";
String jsonStr = JsonFlattener.flatten(json);
System.out.println(jsonStr);
// Output: {"a.b":1,"a.c":null,"a.d[0]":false,"a.d[1]":true,"e":"f","g":2.3}

Find value in JSON HTTP response

I want to get the value of city from the below httpResponse (JSON Response) where fruit is Apple. I am unable to find a way to add this condition to my groovy script.
{
"userInformation": {
"Name": "John",
"Location": "India"
},
"details": [
{
"fruit": "Apple",
"color": "Red",
"city": "New Delhi",
"luckyNumber": 10
},
{
"fruit": "Banana",
"color": "yellow",
"city": "Goa",
"luckyNumber": 12
}
]
}
JsonSlurper is what you need. It parses JSON object to a plain Map which can be then easily navigated to find the desired value.
import groovy.json.JsonSlurper
def input = '''{
"userInformation": {
"Name": "John",
"Location": "India"
},
"details": [
{
"fruit": "Apple",
"color": "Red",
"city": "New Delhi",
"luckyNumber": 10
},
{
"fruit": "Banana",
"color": "yellow",
"city": "Goa",
"luckyNumber": 12
}
]
}
'''
def slurped = new JsonSlurper().parseText(input)
slurped.details.find { it.fruit == 'Apple' }?.city

jsonpath using regular expressions in Talend 5.5

I have this json string:
I want to extract all the ids that are after the node of number:"0","1","2"...etc.
I have succeeded to get a single id by using jsonpath: $.response.data.0.id and got "15124".
but i'm looking for a jsonpath that will extract all the ids in the Json String.
in other words this is the expexted output: 15124,13498,14296,13364,14060,13672.
This is the Json String i have:
{
"response": {
"code": 200,
"msg": "Success",
"data": {
"0": {
"id": "15124",
"name": " yoav (yoavshaki#yahoo.com) - 301519506662355",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 139,
"code": "IST",
"region": "Asia",
"locality": "Jerusalem",
"offset": 3,
"facebook_code": 70
}
},
"1": {
"id": "13498",
"name": "(Not in used) Daniel - 30138444",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
}
},
"2": {
"id": "14296",
"name": "Daniel - ComeOn (bingocafe#walla.com - 1375713835981039)",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
}
},
"3": {
"id": "13364",
"name": "Erez - 116060088528093",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
}
},
"4": {
"id": "14060",
"name": "Erez - NordicBet (gianniciano82#gmail.com - 105134566315107)",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 139,
"code": "IST",
"region": "Asia",
"locality": "Jerusalem",
"offset": 3,
"facebook_code": 70
}
},
"5": {
"id": "13672",
"name": "Erez - alon.dan - 1378526859026272",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
}
}
}
}
}
Thanks for all the helpers!
GSON library is a good option to convert java object to json string and vise versa.
for converting json to java object use: fromJson(String, Class)
for converting java object to json string use: toJson(Object)
Here is the sample code using [Gson#fromJson()] to convert JSON string into java Map.
Find more examples...
Sample code:
BufferedReader reader = new BufferedReader(new FileReader(new File("resources/json.txt")));
Gson gson = new Gson();
Type type = new TypeToken<Map<String, Map<String, Object>>>() {}.getType();
Map<String, Map<String, Object>> map = gson.fromJson(reader, type);
Map<String, Map<String, Object>> innerMap = (Map<String, Map<String, Object>>) map.get("response").get("data");
for (String key : innerMap.keySet()) {
System.out.println("key:" + key + " id:" + innerMap.get(key).get("id"));
}
output:
key:0 id:15124
key:1 id:13498
key:2 id:14296
key:3 id:13364
key:4 id:14060
key:5 id:13672
Thanks Syam S. for your answer.
$.response.data.*.id indeed work!

Categories