Last active
April 19, 2019 14:45
-
-
Save ChadRehmKineticData/5bb382575746bf306ae523db126b217f to your computer and use it in GitHub Desktop.
flatten nested fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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