#K92312. Flatten a Nested Dictionary

    ID: 38170 Type: Default 1000ms 256MiB

Flatten a Nested Dictionary

Flatten a Nested Dictionary

This problem requires you to flatten a nested dictionary. Given a JSON object that may contain nested JSON objects as values, your task is to flatten it by concatenating the keys at different levels with a specified separator. If no separator is specified, use . as the default. The flattening process transforms a nested structure into a single-level JSON object.

For example, given the input dictionary:

{"a": 1, "b": {"c": 2, "d": {"e": 3}}}

the output should be:

{"a": 1, "b.c": 2, "b.d.e": 3}

The key concatenation follows the formula: \( new\_key = \text{parent\_key} \; sep \; \text{key} \) if a parent key exists, and just key otherwise.

inputFormat

The input is provided via standard input. The first line contains a JSON object representing the dictionary. The second line (optional) is a string representing the separator. If the separator is not provided or is an empty string, use "." as the default.

outputFormat

Print the flattened dictionary as a JSON object to standard output.## sample

{"a": 1, "b": 2}
{"a":1,"b":2}