Flatten It

Introduction

Flatten It uses iteration (no recursion) to flatten iterables into a dictionary where each key is a reference path and each value is the value at the respective path.  This simple flattening algorithm has been used for diffing deeply nested JSON structures and for transforming hierarchical data into a tabular data model.

 

Features

  • Produces valid Python or JS reference paths for string and numeric keys.
  • Raises ValueError on circular references.
  • Iterative algorithm; flatten deeply nested structures without causing a call stack overflow.

 

Code

 

Resources

About the Author

Adam Patterson has decades of experience working in software engineering and data engineering roles and he holds a Master’s of Applied Data Science from the University of Michigan.  Adam has worked in IT domains including Education, Health Sciences, and Facilities and Asset Management.  He currently serves as a Data Engineer and Trusted IT Service Provider for the Neurology Department at Michigan Medicine.

 |  |  | 

 

Print Article

Related Articles (3)

Dr. Sarah Sperry and Victoria Murphy of the Emotion and Temporal Dynamics (EmoTe) Lab at the University of Michigan created EMA-CleanR, an R-based program for efficient pre-processing, cleaning, and visualization of Ecological Momentary Assessment (EMA) survey data. This article documents how to use EMA-CleanR and how it works.
This article shows how researchers can make use of an open source data dictionary tool called SchemaSpy to help create professional, easy-to-understand documentation for their datasets. Specific instructions are provided for Oracle, Microsoft SQL Server, CSV, SQlite, R, and Python + Pandas.
Memoiz provides a function decorator that adds memoization to a function or method. It makes reasonable assumptions about how and if to cache the return value of a function or method based on the arguments passed to the callable.