@Test public void testFlattenJsonOnNestedJsonGivingParentPath() throws Exception { TargetModel target = new HdfsTargetModel("test1", hdfsExplorer); List<DataRecord> result = JsonAdapter.flattenJson(dataRecord, nestedJson, target); List<Map<String, String>> expectedColumnsList = new ArrayList<>(); Map<String, String> colNameValuesMap = new HashMap<>(); colNameValuesMap.put("fp", "2017-09"); colNameValuesMap.put("SR1", "Y"); colNameValuesMap.put("A" + PARENT_PATH_SEPARATOR + "B" + PARENT_PATH_SEPARATOR + "AB1", "AB1"); colNameValuesMap.put("A" + PARENT_PATH_SEPARATOR + "B" + PARENT_PATH_SEPARATOR + "AB2", "AB2"); colNameValuesMap.put( "A" + PARENT_PATH_SEPARATOR + "D" + PARENT_PATH_SEPARATOR + "E" + PARENT_PATH_SEPARATOR + "ADE", "ADE"); expectedColumnsList.add(colNameValuesMap); Assert.assertTrue(compareResult(result, expectedColumnsList)); }
public static List<DataRecord> flattenJson(DataRecord dataRecord, String json, TargetModel targetModel) throws InvalidJSONException, ColumnFamilyNotFoundException, InvalidColumnException { List<DataRecord> dataRecordList = new ArrayList<>(); dataRecordList.add(dataRecord); boolean useParentKey = true; JsonElement jsonElement = parser.parse(json); if (jsonElement instanceof JsonObject) { return flattenJson(dataRecordList, (JsonObject) jsonElement, useParentKey, null, targetModel) .getDataRecordList(); } else if (jsonElement instanceof JsonArray) { List<DataRecord> finalDataRecordList = new ArrayList<>(); for (JsonElement jsonElement1 : jsonElement.getAsJsonArray()) { finalDataRecordList.addAll(flattenJson(dataRecord, jsonElement1.toString(), targetModel)); } return finalDataRecordList; } else { throw new InvalidJSONException("Expected a JsonObject, but found " + jsonElement.getClass()); } }
JsonAdapter { public static List<DataRecord> flattenJson(DataRecord dataRecord, String json, TargetModel targetModel) throws InvalidJSONException, ColumnFamilyNotFoundException, InvalidColumnException { List<DataRecord> dataRecordList = new ArrayList<>(); dataRecordList.add(dataRecord); boolean useParentKey = true; JsonElement jsonElement = parser.parse(json); if (jsonElement instanceof JsonObject) { return flattenJson(dataRecordList, (JsonObject) jsonElement, useParentKey, null, targetModel) .getDataRecordList(); } else if (jsonElement instanceof JsonArray) { List<DataRecord> finalDataRecordList = new ArrayList<>(); for (JsonElement jsonElement1 : jsonElement.getAsJsonArray()) { finalDataRecordList.addAll(flattenJson(dataRecord, jsonElement1.toString(), targetModel)); } return finalDataRecordList; } else { throw new InvalidJSONException("Expected a JsonObject, but found " + jsonElement.getClass()); } } }
JsonAdapter { public static List<DataRecord> flattenJson(DataRecord dataRecord, String json, TargetModel targetModel) throws InvalidJSONException, ColumnFamilyNotFoundException, InvalidColumnException { List<DataRecord> dataRecordList = new ArrayList<>(); dataRecordList.add(dataRecord); boolean useParentKey = true; JsonElement jsonElement = parser.parse(json); if (jsonElement instanceof JsonObject) { return flattenJson(dataRecordList, (JsonObject) jsonElement, useParentKey, null, targetModel) .getDataRecordList(); } else if (jsonElement instanceof JsonArray) { List<DataRecord> finalDataRecordList = new ArrayList<>(); for (JsonElement jsonElement1 : jsonElement.getAsJsonArray()) { finalDataRecordList.addAll(flattenJson(dataRecord, jsonElement1.toString(), targetModel)); } return finalDataRecordList; } else { throw new InvalidJSONException("Expected a JsonObject, but found " + jsonElement.getClass()); } }  }
JsonAdapter { public static List<DataRecord> flattenJson(DataRecord dataRecord, String json, TargetModel targetModel) throws InvalidJSONException, ColumnFamilyNotFoundException, InvalidColumnException { List<DataRecord> dataRecordList = new ArrayList<>(); dataRecordList.add(dataRecord); boolean useParentKey = true; JsonElement jsonElement = parser.parse(json); if (jsonElement instanceof JsonObject) { return flattenJson(dataRecordList, (JsonObject) jsonElement, useParentKey, null, targetModel) .getDataRecordList(); } else if (jsonElement instanceof JsonArray) { List<DataRecord> finalDataRecordList = new ArrayList<>(); for (JsonElement jsonElement1 : jsonElement.getAsJsonArray()) { finalDataRecordList.addAll(flattenJson(dataRecord, jsonElement1.toString(), targetModel)); } return finalDataRecordList; } else { throw new InvalidJSONException("Expected a JsonObject, but found " + jsonElement.getClass()); } }  static List<DataRecord> flattenJson(DataRecord dataRecord, String json, TargetModel targetModel); }
JsonAdapter { public static List<DataRecord> flattenJson(DataRecord dataRecord, String json, TargetModel targetModel) throws InvalidJSONException, ColumnFamilyNotFoundException, InvalidColumnException { List<DataRecord> dataRecordList = new ArrayList<>(); dataRecordList.add(dataRecord); boolean useParentKey = true; JsonElement jsonElement = parser.parse(json); if (jsonElement instanceof JsonObject) { return flattenJson(dataRecordList, (JsonObject) jsonElement, useParentKey, null, targetModel) .getDataRecordList(); } else if (jsonElement instanceof JsonArray) { List<DataRecord> finalDataRecordList = new ArrayList<>(); for (JsonElement jsonElement1 : jsonElement.getAsJsonArray()) { finalDataRecordList.addAll(flattenJson(dataRecord, jsonElement1.toString(), targetModel)); } return finalDataRecordList; } else { throw new InvalidJSONException("Expected a JsonObject, but found " + jsonElement.getClass()); } }  static List<DataRecord> flattenJson(DataRecord dataRecord, String json, TargetModel targetModel);  }