Quick Overview

This question evaluates proficiency with JSON data structures, hierarchical traversal, and serialization, testing competency in handling nested objects and arrays, primitive values, and edge cases such as deep nesting and empty containers.

Flatten nested JSON into a string map

Company: Salesforce

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem You are given an input JSON object that may contain nested objects and arrays. Your task is to **flatten** it into a single-level key/value mapping, then **serialize** that mapping into an output string. ### Flattening rules - Each leaf value (string/number/boolean/null) becomes one entry in the flattened map. - Keys in the flattened map represent the full path from the root: - Use `.` to separate nested object keys. - Use `[i]` to represent array indices. - The order of entries in the output does not matter. ### Input - A JSON object (as a string or parsed structure). ### Output - A single string representing the flattened mapping (e.g., JSON string of a flat object, or `key=value` pairs joined by newlines; pick one and document it). ### Example Input JSON: ```json { "a": 1, "b": { "c": 2, "d": [3, 4] }, "e": [{"f": 5}] } ``` One valid flattened result (as a flat JSON object) would be: ```json { "a": 1, "b.c": 2, "b.d[0]": 3, "b.d[1]": 4, "e[0].f": 5 } ``` ### Constraints / Notes - Depth may be large; avoid stack overflow if possible. - Values can be primitive JSON types; nested containers are objects/arrays. - Handle empty objects/arrays consistently (define whether they produce entries or are skipped).

Quick Answer: This question evaluates proficiency with JSON data structures, hierarchical traversal, and serialization, testing competency in handling nested objects and arrays, primitive values, and edge cases such as deep nesting and empty containers.

Given a nested JSON value, flatten every primitive leaf into a single-level mapping. Use `.` between object keys and `[i]` for array indices. Empty objects and arrays produce no entries. Return the flattened mapping as a compact JSON string with keys sorted lexicographically so the result is deterministic.

Constraints

  • The structure contains only JSON-compatible types: object/dict, array/list, string, number, boolean, and null/None.
  • 0 <= total number of container entries and primitive values <= 10^5.
  • Object keys do not contain `.`, `[` or `]`.
  • Empty objects and arrays are skipped and do not create entries in the flattened output.

Examples

Input: ({'a': 1, 'b': {'c': 2, 'd': [3, 4]}, 'e': [{'f': 5}]},)

Expected Output: '{"a":1,"b.c":2,"b.d[0]":3,"b.d[1]":4,"e[0].f":5}'

Explanation: Each primitive leaf becomes one entry: `a`, `b.c`, `b.d[0]`, `b.d[1]`, and `e[0].f`.

Input: ({'x': None, 'y': True, 'z': {}, 'w': [], 'a': {'b': False}},)

Expected Output: '{"a.b":false,"x":null,"y":true}'

Explanation: Only primitive values are emitted. Empty containers `z` and `w` are skipped.

Hints

  1. Use an explicit stack of `(path, value)` pairs instead of recursion so very deep nesting does not cause a recursion-depth error.
  2. Only write to the answer map when the current value is a primitive. If it is a dict or list, extend the path and keep traversing.

Loading coding console...