#C13735. Flatten Nested Dictionary
Flatten Nested Dictionary
Flatten Nested Dictionary
Given a nested dictionary represented in JSON format, your task is to flatten the dictionary so that the keys of nested dictionaries are concatenated into a single key using an underscore as a separator. The flattening should be performed recursively for arbitrarily deep nesting.
For example, the nested dictionary
\[
{"a": 1, "b": {"c": 2, "d": 3}}
\]
should be flattened to
\[
{"a": 1, "b_c": 2, "b_d": 3}
\]
Note that if further nesting occurs, like
\[
{"a":1, "b": {"c": {"d": {"e": 2}}}, "f": {"g":3}}
\]
then the flattened output would be
\[
{"a": 1, "b_c_d_e": 2, "f_g": 3}
\]
Input is provided in JSON format via stdin
and the result should be printed to stdout
as a JSON object.
inputFormat
The input consists of a single line containing a valid JSON object that represents a nested dictionary. Each key is a string and each value is either an integer or another JSON object. You can assume that the input is well-formed.
outputFormat
Output a single JSON object representing the flattened dictionary where nested keys are concatenated using an underscore ('_') as separator.
## sample{"a": 1, "b": 2}
{"a":1,"b":2}