JsonUtils { public static JsonObject flatten(JsonObject obj) { JsonObject result = new JsonObject(); for (String key : obj.fieldNames()) { Object value = obj.getValue(key); if (value instanceof JsonObject) { JsonObject obj2 = (JsonObject)value; obj2 = flatten(obj2); for (String key2 : obj2.fieldNames()) { result.put(key + "." + key2, obj2.getValue(key2)); } } else { result.put(key, value); } } return result; }  static JsonObject flatten(JsonObject obj);  }
@Test public void simple() { JsonObject obj = new JsonObject() .put("type", "Person") .put("person", new JsonObject() .put("firstName", "Clifford") .put("lastName", "Thompson") .put("age", 40) .put("address", new JsonObject() .put("street", "First Street") .put("number", 6550))); JsonObject expected = new JsonObject() .put("type", "Person") .put("person.firstName", "Clifford") .put("person.lastName", "Thompson") .put("person.age", 40) .put("person.address.street", "First Street") .put("person.address.number", 6550); assertEquals(expected, JsonUtils.flatten(obj)); }