Flatten JSON into Repeated Key-Value Pairs
Updated
Problem statement
Flatten a nested JSON value into an ordered list of key-value pairs. Join nested object keys with dots. Arrays add no path segment: their scalar leaves repeat the current path, even when several pairs have identical keys or values.
Input representation
The input is a table called nodes, of type String[][]. It describes the JSON tree the same way in every language. Row 0 is the root. The JSON has already been parsed into this table. You do not need to parse JSON text.
["OBJECT", key1, child1, key2, child2, ...]is an object. Each key is followed by its child-node index. The pair order is the object's member order.["ARRAY", child1, child2, ...]is an array with children in element order.["VALUE", scalarJson]is one scalar.scalarJsonis a valid JSON encoding of a string, number, boolean, or null. Copy this text unchanged into the output; do not decode or change it.
Flattening rules
- Begin with an empty path.
- Visit object members in their encoded order. Append the member key to the path, separated by a dot when the path is already nonempty, and traverse its value.
- Visit array elements from left to right using the same path. Do not add array indexes.
- At a VALUE row, append
[path, scalarJson]to the answer. - Empty objects and arrays contribute no output. Restore the parent path before visiting the next sibling.
Return String[][] in depth-first encounter order. Keep repeated paths and repeated identical pairs. Every scalar leaf is guaranteed to have at least one object-key ancestor, so every emitted path is nonempty.
Follow-up
After solving the flattening task, explain whether the pair list uniquely determines the original JSON and what extra structure a format would need to let you rebuild the original JSON. Your function returns only the flattened pairs. It does not rebuild the JSON.
Function
flattenJsonPairs(nodes: String[][]) → String[][]Examples
Example 1
nodes = [["OBJECT","a","1"],["OBJECT","b","2","c","3"],["VALUE","\"foo\""],["ARRAY","4","5","6"],["VALUE","4"],["VALUE","5"],["VALUE","6"]]return = [["a.b","\"foo\""],["a.c","4"],["a.c","5"],["a.c","6"]]In Python terms, flatten({'a': {'b': 'foo', 'c': [4,5,6]}}) = [('a.b', 'foo'), ('a.c', 4), ('a.c', 5), ('a.c', 6)]. The node table encodes that same input. The second output column uses JSON text, so the string value foo includes its JSON quotes. Array elements repeat a.c; no index is added.
Example 2
nodes = [["ARRAY","1","5"],["OBJECT","z","2","a","3"],["VALUE","2"],["OBJECT","x","4"],["VALUE","3"],["OBJECT","z","6"],["VALUE","4"]]return = [["z","2"],["a.x","3"],["z","4"]]The root array contributes no path segment. Visit the first object in its encoded order, then the second object. The two z rows remain separate.
Example 3
nodes = [["OBJECT","items","1","empty","6"],["ARRAY","2","3","4","5"],["ARRAY"],["OBJECT"],["VALUE","\"x\""],["VALUE","null"],["OBJECT"]]return = [["items","\"x\""],["items","null"]]Empty arrays and objects emit no pair. The string and null leaves retain the same items path and their different JSON scalar encodings.
Constraints
1 <= nodes.length <= 10000. The tree has at most100edges on a root-to-leaf path.- Every row is well formed. Child indexes are decimal integers that name later rows; every nonroot row has exactly one parent and every row is reachable from row
0. - The root is OBJECT or ARRAY. An ARRAY root contains at least one OBJECT descendant. Every scalar leaf has at least one object-key ancestor.
- Keys contain
1to30ASCII letters, digits, or underscores, so keys never contain dots. Keys are unique within each object. - Each scalar token has at most
1000characters. The combined input-field length and the combined output-string length are each at most200000.
More Klaviyo interview questions
- Render Sidebar Labels as HTMLONSITE INTERVIEW · Seen Jul 2025
- Trace Logs at a Two-Second CadenceONSITE INTERVIEW · Seen Jul 2025
- Validate Input Against Length BoundsONSITE INTERVIEW · Seen Jul 2025
- Conditionally Insert or Update RecordsONSITE INTERVIEW · Seen Jul 2022
- Progressive Parcel Tracking OperationsOA · Seen Aug 2026
- Banking Transfers, Account Merges, and Historical BalancesOA · Seen Jul 2025
- In-Memory Database with Permanent and Expiring ValuesOA · Seen Jul 2025
- File Storage with User Quotas, Merging, Backups, and RestoreOA · Seen Jun 2025
- Key-Value Locks with Waiting QueuesOA · Seen Sep 2024
Similar questions at other companies
- Filesystem Entity Total SizeGoogle · ONSITE INTERVIEWGoogle interview questions
- Render a Comment HierarchyAmazon · ONSITE INTERVIEW
- Kth Smallest Element in a Binary Search TreeSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Delete a Filesystem TreeDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- Flatten and Reconstruct an Ordered TreeThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- Inorder, Preorder and Postorder TraversalsAMD · ONSITE INTERVIEWAMD interview questions
- Compare N-Ary Tree Leaf SequencesNavan · ONSITE INTERVIEWNavan interview questions
- Kth Smallest Value in a BSTExpedia · PHONE SCREEN · ONSITE INTERVIEWExpedia interview questions
- Validate Binary Search TreeInMobi · ONSITE INTERVIEWInMobi interview questions
- Serialize a Binary Search TreeConfluent · ONSITE INTERVIEWConfluent interview questions