How to transform complex nested structures into simple, flat