Source:
<code>private void manipulatingFlattenJson(JsonElement in, String prefix) { if (in.isJsonObject()) { JsonObject obj = in.getAsJsonObject(); if (StringUtils.countMatches(prefix, ".") > 1) { addHeuristicValue(obj, prefix); return; } for (Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); manipulatingFlattenJson(value, appendPrefix(prefix, key)); } } else if (in.isJsonArray()) { JsonArray arrayObj = in.getAsJsonArray(); <START> boolean isAllJsonObject = arrayObj.size() > 0 ? StreamSupport.stream(arrayObj.spliterator(), false).allMatch(x -> x.isJsonObject()) : false; <END> if (isAllJsonObject) { Map<String, Integer> occurents = new HashMap<>(); for (JsonElement element : arrayObj) { JsonObject obj = element.getAsJsonObject(); for (Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); occurents.merge(key, 1, Integer::sum); } } JsonObject newObj = new JsonObject(); for (String key : occurents.keySet()) { if (newObj.get(key) == null) { newObj.add(key, new JsonArray()); } StreamSupport.stream(arrayObj.spliterator(), false).forEach(x -> { newObj.get(key).getAsJsonArray().add(x.getAsJsonObject().get(key)); }); } manipulatingFlattenJson(newObj, prefix); } else { flatten.add(prefix, new JsonPrimitive("String value")); } } else if (in.isJsonPrimitive()) { flatten.add(prefix, in.getAsJsonPrimitive()); } else { flatten.add(prefix, JsonNull.INSTANCE); } }</code><technical_language>Code style: simpler write this instead of ternary: arrayObj.size() > 0 && StreamSupport.stream(arrayObj.spliterator(), false).allMatch(x -> x.isJsonObject()); purely personal taste, I nicer: x -> x.isJsonObject() replaced a method reference JsonElement::isJsonObject; StreamSupport.stream import statically, stream() method is obvious does</technical_language>

Target:
private void manipulatingFlattenJson(JsonElement in, String prefix) { if (in.isJsonObject()) { JsonObject obj = in.getAsJsonObject(); if (StringUtils.countMatches(prefix, ".") > 1) { addHeuristicValue(obj, prefix); return; } if (obj.entrySet().isEmpty()) { flatten.add(prefix, obj); } else { for (Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); manipulatingFlattenJson(value, appendPrefix(prefix, key)); } } } else if (in.isJsonArray()) { JsonArray arrayObj = in.getAsJsonArray(); boolean isAllJsonObject = arrayObj.size() > 0 && StreamSupport.stream(arrayObj.spliterator(), false).allMatch(JsonElement::isJsonObject); if (isAllJsonObject) { Map<String, Integer> occurents = new HashMap<>(); for (JsonElement element : arrayObj) { JsonObject obj = element.getAsJsonObject(); for (Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); occurents.merge(key, 1, Integer::sum); } } JsonObject newObj = new JsonObject(); for (String key : occurents.keySet()) { newObj.add(key, new JsonArray()); for (JsonElement elem : arrayObj) { newObj.get(key).getAsJsonArray().add(elem.getAsJsonObject().get(key)); } } manipulatingFlattenJson(newObj, prefix); } else { flatten.add(prefix, new JsonPrimitive("String value")); } } else if (in.isJsonPrimitive()) { flatten.add(prefix, in.getAsJsonPrimitive()); } else { flatten.add(prefix, JsonNull.INSTANCE); } }