Flatten Nested JSON

Difficulty: Medium · Topics: JSON, Recursion, Map · Acceptance: 100.0% of 1 submissions

Difficulty: Medium  |  Tags: JSON, Recursion, Map

Description

Given a nested JSON object, flatten it so that all keys become dot-separated paths from root.

Implement:

flatten(json) → Map<String, Object>

Examples

Example 1:

Input:
  {"user": {"name": "Alice", "address": {"city": "NYC", "zip": "10001"}}}

Output:
  {"user.name": "Alice", "user.address.city": "NYC", "user.address.zip": "10001"}

Example 2 — With arrays:

Input:
  {"tags": ["java", "python"], "meta": {"count": 2}}

Output:
  {"tags[0]": "java", "tags[1]": "python", "meta.count": 2}

Example 3:

Input:  {"a": 1}
Output: {"a": 1}

Constraints

Follow-up

Practice this problem

Related problems