Problem
You are given an input JSON object that may contain nested objects and arrays. Your task is to flatten it into a single-level key/value mapping, then serialize that mapping into an output string.
Flattening rules
-
Each leaf value (string/number/boolean/null) becomes one entry in the flattened map.
-
Keys in the flattened map represent the full path from the root:
-
Use
.
to separate nested object keys.
-
Use
[i]
to represent array indices.
-
The order of entries in the output does not matter.
Input
-
A JSON object (as a string or parsed structure).
Output
-
A single string representing the flattened mapping (e.g., JSON string of a flat object, or
key=value
pairs joined by newlines; pick one and document it).
Example
Input JSON:
{
"a": 1,
"b": {
"c": 2,
"d": [3, 4]
},
"e": [{"f": 5}]
}
One valid flattened result (as a flat JSON object) would be:
{
"a": 1,
"b.c": 2,
"b.d[0]": 3,
"b.d[1]": 4,
"e[0].f": 5
}
Constraints / Notes
-
Depth may be large; avoid stack overflow if possible.
-
Values can be primitive JSON types; nested containers are objects/arrays.
-
Handle empty objects/arrays consistently (define whether they produce entries or are skipped).