#C5296. Flatten Nested Dictionary

    ID: 48929 Type: Default 1000ms 256MiB

Flatten Nested Dictionary

Flatten Nested Dictionary

You are given a nested JSON object (dictionary) and an optional separator. Your task is to flatten the dictionary. In the flattened dictionary, each key from the original nested dictionary is concatenated using the provided separator (or a period . by default) to form the new keys.

For example, given the input dictionary:

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

the flattened output is:

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

Note: If a separator is provided on the second line of input, use it instead of the default period. The input is read from standard input and the output must be printed to standard output in JSON format.

The flattening is done recursively. For any key whose value is an object, its nested keys are combined with the parent key using the separator. Other types of values (such as numbers, strings, booleans, arrays, or null) should remain unchanged.

You may assume that the input JSON is valid and the keys are strings.

All formulas (if any) should be expressed in \(\LaTeX\) format. In our case, no formulas are needed.

inputFormat

The input consists of one or two lines read from standard input.

  • The first line is a JSON string representing a dictionary. This dictionary can be nested.
  • The second line (optional) is a string representing the separator. If this line is missing or empty, the default separator . should be used.

outputFormat

Output the flattened dictionary in JSON format to standard output. The keys in the output must be the concatenation of nested keys separated by the given separator.

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