#K85107. Flatten Nested Dictionary
Flatten Nested Dictionary
Flatten Nested Dictionary
You are given a nested dictionary represented in JSON format. Your task is to flatten this dictionary so that all nested keys are concatenated into a single key with each level separated by a dot ('.').
For example, given the dictionary
{"a": 1, "b": {"c": 2, "d": {"e": 3}}, "f": {"g": 4}}
the flattened result should be
{"a": 1, "b.c": 2, "b.d.e": 3, "f.g": 4}
The key concatenation follows the formula below:
$$ key = parent \; key + "." + current \; key $$
If the dictionary is empty or there is no nesting, simply output the dictionary as is.
Input is provided via standard input (stdin) as a single line JSON string and the output should be printed to standard output (stdout) as a JSON string without extra spaces.
inputFormat
The input consists of a single line representing a valid JSON object (dictionary). The keys are strings and the values may be integers or nested dictionaries.
Example:
{"a": 1, "b": {"c": 2, "d": {"e": 3}}, "f": {"g": 4}}
outputFormat
Output a JSON string representing the flattened dictionary. The keys in the flattened dictionary are the paths to the corresponding values in the original dictionary, joined by a dot.
Example:
{"a":1,"b.c":2,"b.d.e":3,"f.g":4}## sample
{"a": 1, "b": {"c": 2, "d": {"e": 3}}, "f": {"g": 4}}
{"a":1,"b.c":2,"b.d.e":3,"f.g":4}