1 of 4
Turn a nested document into rows, and know which grain the number you just computed belongs to.
Flatten this order for a table. The nested customer becomes columns; the items list stays where it is.
179 characters, checked against 7 rules
Records and formats· 3 of 3
Shapes that nest
JSON carries types and nests, which solves the quoting problem and creates the flattening problem. Deciding how a document becomes a row is a design decision, not a mechanical one.
25 minute read
Flat files treat every column as a scalar, often serialized as text. API payloads frequently deliver structured, hierarchical documents:
{
"order_id": 1003,
"customer": {"email": "lin@example.com", "country": "SG"},
"items": [
{"sku": "A-1", "quantity": 2},
{"sku": "B-7", "quantity": 1}
],
"total": 1499.00
}
This format provides two distinct advantages over CSV. Numeric values such as 1499.00 retain native numeric semantics without delimiter ambiguity. Sub-objects such as customer and nested collections such as items maintain explicit boundaries.
Converting hierarchical documents into tabular records requires specific structural decisions.
Flattening is a structural decision
Transforming nested structures into relational rows requires selecting an appropriate representation strategy:
- Dotted key flattening: Nested dictionary keys become flat column names, producing columns such as
customer.emailandcustomer.country. This strategy handles single nested objects, but fails on array fields likeitems. - Record explosion: Unnesting arrays into multiple rows emits one record per array element. For order 1003, this produces two rows, one for
A-1and one forB-7. While item details remain distinct, root-level attributes liketotalare duplicated across rows. An unweighted sum acrosstotalyields 2998 instead of 1499. - Serialized storage: Storing the array as raw JSON text within a single column retains root-level cardinality, but obscures nested attributes from relational query engines.
The standard relational pattern splits this data across two related entities: an orders parent table and an order_items child table linked by foreign key. When flattening occurs earlier in a pipeline, data engineers must track aggregation levels to prevent double-counting.
Type preservation and edge cases in JSON
JSON supports strings, numbers, booleans, arrays, objects, and null. While this reduces string-parsing ambiguities, type handling still presents edge cases:
- Missing keys versus null values: A missing key indicates the attribute was omitted at the source, while an explicit
nullindicates the value was known to be empty. Flattening transformations often collapse this distinction into a single null representation. - Floating-point precision: JSON numbers do not enforce fixed decimal precision. Monetary quantities deserialized into floating-point variables can introduce fractional rounding errors. Financial applications store monetary amounts as string representations of exact decimals or as integers in minor currency units (such as cents).
What the exercise asks
Implement three operations:
- Flatten nested dictionaries using dotted key paths.
- Explode nested list fields into distinct rows while preserving parent identifiers.
- Construct a validation check that identifies inflation when summing parent attributes across exploded rows.
Detecting grain mismatches early prevents silent aggregation bugs in downstream transformations.
Shapes that nest
JSON carries types and nests, which solves the quoting problem and creates the flattening problem. Deciding how a document becomes a row is a design decision, not a mechanical one.
25 minute read
Implement your solution in the editor on the right or work in your local IDE. All unit tests run against your code in real-time.
Exercise
flatten-records
25 tests, graded on your machine in about a second. Stuck for 25 minutes? The desktop app will offer you the reference solution.