#C8132. Flatten Dictionary
Flatten Dictionary
Flatten Dictionary
You are given a nested dictionary d and a separator string s. Your task is to flatten the dictionary by concatenating nested keys into a single key using the separator. More formally, for a nested key sequence \(k_1, k_2, \dots, k_n\) with value \(v\), the resulting dictionary should have an entry with key \(k_1 s k_2 s \dots s k_n\) and value \(v\). Note that if a nested dictionary is empty, it should be skipped.
For example, if \(d = {"a":1, "b": {"c": 2, "d": {"e": 3}}}\) and the separator is .
, the flattened dictionary is
\( {"a":1, "b.c":2, "b.d.e":3} \).
The input is provided via standard input (stdin
) and the flattened dictionary must be output in JSON format on standard output (stdout
).
inputFormat
The input consists of two parts provided through stdin
:
- The first line contains the separator string
s
. - The remaining lines form a JSON string representing the dictionary
d
.
You can assume that the JSON represents an object whose values are either integers or nested objects.
outputFormat
Output to stdout
a single line containing the flattened dictionary in JSON format.
.
{"a": 1, "b": {"c": 2, "d": {"e": 3}}}
{"a":1,"b.c":2,"b.d.e":3}