- if isinstance(parent, dict): # the parent is top-level
- documents.pop(part)
- parent['parts'].append(child)
- documents[part] = child['parts']
- else: # not top-level
- parent.append(child)
-
- return {
- 'documents': [d for d in documents.itervalues() if isinstance(d, dict)]
- }