@Test public void testFlattenJsonForFetchingSomeRootAndNonRootElements() throws Exception { TargetModel target = new HdfsTargetModel("test5", hdfsExplorer); List<DataRecord> result = JsonAdapter.flattenJson(dataRecord, jsonHavingMultipleList, target); List<Map<String, String>> expectedColumnsList = new ArrayList<>(); Map<String, String> commonColumnValuesMap = new HashMap<>(); commonColumnValuesMap.put("fp", "2017-09"); commonColumnValuesMap.put("SR1", "Y"); commonColumnValuesMap.put("A", "A"); Map<String, String> otherColumnValuesMap = new HashMap<>(); otherColumnValuesMap.putAll(commonColumnValuesMap); otherColumnValuesMap.put("B" + PARENT_PATH_SEPARATOR + "BD", "BD"); otherColumnValuesMap.put("C" + PARENT_PATH_SEPARATOR + "CE", "CE"); expectedColumnsList.add(otherColumnValuesMap); otherColumnValuesMap = new HashMap<>(); otherColumnValuesMap.putAll(commonColumnValuesMap); otherColumnValuesMap.put("B" + PARENT_PATH_SEPARATOR + "BD", "BD"); otherColumnValuesMap.put("C" + PARENT_PATH_SEPARATOR + "CF", "CF"); expectedColumnsList.add(otherColumnValuesMap); otherColumnValuesMap = new HashMap<>(); otherColumnValuesMap.putAll(commonColumnValuesMap); otherColumnValuesMap.put("B" + PARENT_PATH_SEPARATOR + "BE", "BE"); otherColumnValuesMap.put("C" + PARENT_PATH_SEPARATOR + "CE", "CE"); expectedColumnsList.add(otherColumnValuesMap); otherColumnValuesMap = new HashMap<>(); otherColumnValuesMap.putAll(commonColumnValuesMap); otherColumnValuesMap.put("B" + PARENT_PATH_SEPARATOR + "BE", "BE"); otherColumnValuesMap.put("C" + PARENT_PATH_SEPARATOR + "CF", "CF"); expectedColumnsList.add(otherColumnValuesMap); 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);  }