Skip to content

Instantly share code, notes, and snippets.

@ChadRehmKineticData
Last active April 19, 2019 14:45
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 ChadRehmKineticData/5bb382575746bf306ae523db126b217f to your computer and use it in GitHub Desktop.
Save ChadRehmKineticData/5bb382575746bf306ae523db126b217f to your computer and use it in GitHub Desktop.
flatten nested fields
private static final Pattern NESTED_PATTERN = Pattern.compile("(.*?)\\[(.*?)\\]");
public List<Record> flattenNestedFields(List<String> fields, List<Record> records)
throws BridgeError{
// Build the list of map/hash fields to flatten (this is a map of field
// name, to a set of field keys)
Map<String,Set<String>> nestedFields = new TreeMap<>();
for (String field : fields) {
Matcher matcher = NESTED_PATTERN.matcher(field);
if (matcher.matches()) {
nestedFields.computeIfAbsent(matcher.group(1), key ->
new TreeSet()).add(matcher.group(2));
}
}
// For each record
for (Record record : records) {
// For each of the nested fields to flatten
for (Map.Entry<String,Set<String>> entry : nestedFields.entrySet()) {
// For each of the keys to flatten
for (String key : entry.getValue()) {
Map<String,Object> recordMap = record.getRecord();
Object obj = ((JSONObject)recordMap).get(entry.getKey());
// attributes path
if (obj instanceof JSONArray) {
recordMap.put(key,getAttributeValues(entry.getKey(), key,
(JSONObject)recordMap));
record.setRecord(recordMap);
}
// values path
if (obj instanceof JSONObject) {
recordMap.put(key, ((JSONObject) obj).get(obj));
record.setRecord(recordMap);
}
}
}
}
return records;
}
private List getAttributeValues(String type, String name, JSONObject object)
throws BridgeError {
if (!object.containsKey(type)) throw new BridgeError(
String.format("The field '%s' cannot be found on the object",
type));
JSONArray attributes = (JSONArray)object.get(type);
for (Object attribute : attributes) {
HashMap attributeMap = (HashMap)attribute;
if (((String)attributeMap.get("name")).equals(name)) {
return (List)attributeMap.get("values");
}
}
return new ArrayList(); // Return an empty list if no values were found
}
JSONParser parser = new JSONParser();
String form = "[{"
+ "\"attributes\":"
+ "[{"
+ "\"name\":\"Icon\","
+ "\"values\":[\"fa-truck\"]},"
+ "{\"name\":\"Owning Team\","
+ "\"values\":[\"Facilities\"]"
+ "}],"
+ "\"name\":\"Cleaning\""
+ "}]";
String submission = "[{"
+ "\"coreState\":\"Draft\","
+ "\"createdBy\":\"joe.bar@foo.com\","
+ "\"id\":\"3250911c-5afc-11e9-bf69-29dd7c482cf1\","
+ "\"values\":{"
+ "\"Status\":\"Draft\","
+ "\"Requested For\":\"joe.bar@foo.com\""
+ "}"
+ "}]";
@Test
public void test_form() throws Exception {
List<String> fields = new ArrayList();
fields.add("name");
fields.add("attributes[Icon]");
Object obj = parser.parse(form);
JSONArray JSONrecords = (JSONArray)obj;
List<Record> records = new ArrayList<Record>();
for (Object o : JSONrecords) {
records.add(new Record((Map)o));
}
KineticCoreApiHelper helper = new KineticCoreApiHelper("user","pass","instance");
helper.flattenNestedFields(fields, records);
int x = 1;
}
@Test
public void test_submission() throws Exception {
List<String> fields = new ArrayList();
fields.add("id");
fields.add("values[Status]");
Object obj = parser.parse(submission);
JSONArray JSONrecords = (JSONArray)obj;
List<Record> records = new ArrayList<Record>();
for (Object o : JSONrecords) {
records.add(new Record((Map)o));
}
KineticCoreApiHelper helper = new KineticCoreApiHelper("user","pass","instance");
helper.flattenNestedFields(fields, records);
int x = 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment