#C14485. Flatten Dictionary
Flatten Dictionary
Flatten Dictionary
You are given a nested dictionary where each key is a string and each value is either an integer or another dictionary. Your task is to write a program that flattens this dictionary so that the nested keys are concatenated into a single key using a given separator.
Specifically, if an element in the dictionary is another dictionary, then its keys should be appended to the parent key with the separator in between. If the second line of input (the separator) is empty, use .
as the default separator.
For example, given the dictionary {"a":1, "b":{"c":2, "d":3}}
and an empty separator, the flattened dictionary is {"a":1, "b.c":2, "b.d":3}
. Similarly, if the separator is +
, the output will be {"a":1, "b+c":2, "b+d":3}
.
Your output must be in valid JSON format.
inputFormat
The input consists of two lines provided via stdin:
- The first line is a JSON-formatted string representing a nested dictionary. The keys will be strings and the values will be either integers or nested dictionaries.
- The second line is a separator string. If this line is empty, use "." as the default separator.
outputFormat
Output the flattened dictionary in JSON format to stdout.## sample
{"a":1,"b":2}
{"a":1,"b":2}