Skip to content

Instantly share code, notes, and snippets.

@japharr
Created February 6, 2022 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save japharr/48f54a2c7559f2d888b7a504d349f07f to your computer and use it in GitHub Desktop.
Save japharr/48f54a2c7559f2d888b7a504d349f07f to your computer and use it in GitHub Desktop.
Flatten JsonObject
private Stream<Map.Entry<String, Object>> recursiveFlattening(String key, Map.Entry<String, Object> entry) {
String jsonKey = entry.getKey();
Object jsonElement = entry.getValue();
if(key != null)
jsonKey = key + "/" + jsonKey;
final String res = jsonKey;
if (jsonElement instanceof JsonObject) {
return ((JsonObject) jsonElement).stream()
.flatMap(item -> recursiveFlattening(res, item));
} else if (jsonElement instanceof JsonArray) {
return ((JsonArray) jsonElement).stream()
.map(v -> Map.entry("", v))
.flatMap(item -> recursiveFlattening(res, item));
} else {
return Stream.of(Map.entry(res, entry.getValue()));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment