#K81342. Flatten a Nested Dictionary
Flatten a Nested Dictionary
Flatten a Nested Dictionary
Given a nested dictionary of arbitrary depth, your task is to flatten it by concatenating nested keys using a specified separator. For example, if a key k
appears inside another key j
and the separator is .
, then they should be combined into j.k
. If a nested dictionary is empty, it is omitted in the output.
The default separator is given by \(\texttt{.}\). Use the provided separator if one is given; otherwise, default to \(\texttt{.}\).
Note: The input is provided via standard input and the output should be printed to standard output. The input consists of two lines. The first line is a JSON string representing the nested dictionary, and the second line is the separator string. If the separator input is an empty line, then the default separator \(\texttt{.}\) should be used.
inputFormat
The input consists of two lines:
- The first line is a JSON object representing the nested dictionary. All keys are strings and values can be integers or dictionaries.
- The second line is a string denoting the separator. If this line is empty, use the default separator
.
.
outputFormat
Output a JSON object (to stdout) representing the flattened dictionary, where each nested key is concatenated using the specified separator.
## sample{"a": 1, "b": 2}
{"a":1,"b":2}