#C4875. Flatten Nested Dictionary

    ID: 48461 Type: Default 1000ms 256MiB

Flatten Nested Dictionary

Flatten Nested Dictionary

Given a nested dictionary in JSON format where the values are either integers or nested dictionaries, your task is to flatten it. In the flattened dictionary, keys from each nesting level are concatenated with a period (.). Mathematically, if a key has a prefix, then the new key is given by \( new\_key = prefix + "." + key \); otherwise, it remains just \( key \).

For example, given the input:

{"key1": {"keyA": 1, "keyB": 2, "keyC": {"keyD": 3}}, "key2": 4}

The output should be:

{"key1.keyA": 1, "key1.keyB": 2, "key1.keyC.keyD": 3, "key2": 4}

Your solution should read the dictionary from stdin and output the flattened dictionary as a JSON string to stdout.

inputFormat

The input is provided via standard input (stdin) as a single line containing a valid JSON object. The object represents a dictionary where keys are strings and values are either integers or nested JSON objects.

Examples:

{"key1": {"keyA": 1, "keyB": 2}, "key2": 3}

outputFormat

Print to standard output (stdout) the flattened dictionary as a single line JSON string. The keys at different nesting levels should be joined by a period (.).

For example, for the input:

{"key1": {"keyA": 1, "keyB": 2}, "key2": 3}

The correct output is:

{"key1.keyA":1,"key1.keyB":2,"key2":3}
## sample
{"key1": {"keyA": 1, "keyB": 2}, "key2": 3}
{"key1.keyA":1,"key1.keyB":2,"key2":3}