#C11760. Flatten Nested Dictionary

    ID: 41112 Type: Default 1000ms 256MiB

Flatten Nested Dictionary

Flatten Nested Dictionary

You are given a nested dictionary in JSON format. Your task is to flatten the dictionary so that the result is a single-level dictionary. In the flattened dictionary, the keys are formed by concatenating the keys from each level using a separator.

For example, given the nested dictionary

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

and using the separator _, the output should be:

{\"a\":1,\"b_c\":2,\"b_d_e\":3}

If a custom separator is provided, use it instead of the default underscore _.

The input will be provided via standard input in the following format:

  • The first line contains the JSON representation of a dictionary. The dictionary may contain nested dictionaries as values and integer values as leaves.
  • The second line (optional) is a string denoting the separator. If this line is missing or empty, use _ as the separator.

Your program should output the flattened dictionary as a JSON string on standard output.

inputFormat

The input consists of one or two lines:

  • Line 1: A JSON string representing a nested dictionary. The keys will be strings and the leaves will be integers.
  • Line 2 (optional): A string representing the separator to be used when concatenating keys. If this line is missing or empty, use an underscore _ as the default separator.

outputFormat

Output a single line containing the flattened dictionary in JSON format. The keys should be concatenated according to the provided (or default) separator.

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