#K5221. Flatten Nested JSON

    ID: 29259 Type: Default 1000ms 256MiB

Flatten Nested JSON

Flatten Nested JSON

Given a nested JSON object and a delimiter string, your task is to flatten the object into a single-level dictionary. The keys in the flattened dictionary should represent the path to every terminal value in the original object. For objects (dictionaries), the keys are concatenated using the given delimiter. For arrays (lists), use square brackets to denote the index.

Formally, if an element is nested inside another object or array, its key in the flattened dictionary is constructed as follows: if the parent key is ( K ) and the child key or index is ( k ), then the new key is given by ( K \text{delimiter} k ) for objects and ( K[k] ) for arrays. Your program should read the input from standard input and output the flattened JSON to standard output in JSON format.

inputFormat

The input consists of two lines read from standard input (stdin). The first line is a valid JSON object (which may be nested), and the second line is a non-empty delimiter string enclosed in quotes.

Example:

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

outputFormat

Output the flattened JSON object in JSON format to standard output (stdout). The order of keys in the output does not matter.

Example output for the sample input above: {"a.b.c":1,"d":2}## sample

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