как в методе на входе соединить два JSON и если у них одинаковые Uuid объединить в один и после парсить обратно, чтоб на выходе был один слитый JSON

Как пример два JSON на входе. Как соединить два в один таким образом чтоб одинаковые объединились и amount прибавился и отдать обратно с метода JSON?

1:
[{
    "productUuid": "afcf6698-66ef-443f-bb71-810851dce3dd",
    "productName": "Product675",
    "amount": 47
    },
    {
        "productUuid": "7cdf6c23-88e2-4e5c-b7fc-f8546e677700",
        "productName": "Product688",
        "amount": 14
    }]
2:
 [{
    "productUuid": "afcf6698-66ef-443f-bb71-810851dce3dd",
    "productName": "Product675",
    "amount": 35
    },
    {
   "productUuid": "9b3e9312-b5f8-4d15-a291-4db1803cddac",
   "productName": "Product347",
   "amount": 71
        }]

Ответы (1 шт):

Автор решения: Aleksandr Kuzminchuk
public void joinTwoDataSourcesUrlandFile(String urlMassage, String file) {
    
    String allGoods = null;

    List<Goods> goodsWithFile = new ArrayList<>();

    List<Goods> goodsWithInternet = new ArrayList<>();

    List<Goods> merged = new ArrayList<>();

    ResponseEntity<List<Goods>> responseEntity = restTemplate.exchange(urlMassage, HttpMethod.GET, null,
            new ParameterizedTypeReference<List<Goods>>() {
            });

    goodsWithInternet = responseEntity.getBody();

    try (FileReader reader = new FileReader(file)) {

        Type type = new TypeToken<List<Goods>>() {
        }.getType();
        goodsWithFile = gson.fromJson(reader, type);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    merged = merged(goodsWithFile, goodsWithInternet);
    
    try {
        allGoods = mapper.writeValueAsString(merged);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    System.out.println(allGoods);
}
public static List<Goods> merged(List<Goods> fromFile, List<Goods> fromWeb) {

    List<Goods> raw = new ArrayList<>();
    raw.addAll(fromFile);
    raw.addAll(fromWeb);

    Map<String, Goods> toReturn = new HashMap<>();
    
    raw.forEach(rawProduct -> {
        if (toReturn.get(rawProduct.getProductUuid()) == null) {
            toReturn.put(rawProduct.getProductUuid(), rawProduct);
        }else {
            Goods toChange = toReturn.get(rawProduct.getProductUuid());
            int newAmount = toChange.getAmount() + rawProduct.getAmount();
            toReturn.get(rawProduct.getProductUuid()).setAmount(newAmount);
            
        }
    });

    return new ArrayList<>(toReturn.values());
}
→ Ссылка