#K78592. Flatten a Nested Dictionary

    ID: 35120 Type: Default 1000ms 256MiB

Flatten a Nested Dictionary

Flatten a Nested Dictionary

Given a nested dictionary (in JSON format) and a delimiter, your task is to flatten the dictionary. Flattening a dictionary means converting a dictionary that may contain nested dictionaries into a single level dictionary by concatenating the keys from each level using the given delimiter.

For example, given the dictionary {"a": {"b": 1}} with the delimiter ., the flattened dictionary is {"a.b": 1}.

The flattening is done recursively. If no delimiter is provided (i.e. an empty line is given), use the default delimiter ..

In this problem, you will read two lines from standard input (stdin): the first line is the JSON representation of a nested dictionary; the second line is the delimiter string. Your program should output (to stdout) the flattened dictionary in JSON format.

The key concatenation for nested keys should follow the formula:

$$key_{new} = \begin{cases} key & \text{if } parent\_key \text{ is empty} \\ parent\_key + delimiter + key & \text{otherwise} \end{cases} $$

inputFormat

The input consists of two lines:

  1. The first line is a JSON string representing the nested dictionary. The dictionary maps string keys to either integers or another dictionary of the same form.
  2. The second line is a string denoting the delimiter. If this line is empty, the default delimiter . should be used.

outputFormat

The output is a single line containing a JSON string of the flattened dictionary.

## sample
{"a":1,"b":2}
.
{"a":1,"b":2}