Flatten a Nested JSON Object
Problem Read a nested JSON object and flatten it into dot-notation key/value pairs, where each leaf's key is its full path from the root joined by dots.
Input / Output
- Input: a parsed JSON object containing arbitrarily nested sub-objects.
- Output: a flat map of dotted key → primitive value, with exactly one entry per leaf.
Constraints
- Nesting depth is arbitrary; only leaves (primitives) appear in the output.
- Settle the contract on the ambiguous cases first: how arrays are keyed (index-suffixed like
a.0.b, or treated as opaque leaf values), what an empty object maps to (dropped, or a leaf with an empty value), and whether keys may themselves contain a literal dot — which makes the flattening lossy and non-invertible. - Null values are leaves and must be preserved, not skipped.
Example Input:
a: {
a1: 123,
a2: {
b: "abc",
c: 32
}
}
Output:
a.a1 = 123
a.a2.b = "abc"
a.a2.c = 32
- Note that
aanda.a2never appear as output keys in their own right — only leaves are emitted.
asked …