Decode Form-Encoded API Parameters Into Nested Objects and Lists
Last updated: November 29, 2026
Quick Overview
Stripe's API accepts `application/x-www-form-urlencoded` bodies with bracket syntax such as `metadata[order_id]=6735`, `expand[]=customer` and `items[0][price]=price_1`. Parse such a body into nested dicts and lists, URL-decoding keys and values and rejecting conflicting shapes.
Stripe
November 29, 20260
0
0 solved
Stripe's API accepts `application/x-www-form-urlencoded` bodies with bracket syntax such as `metadata[order_id]=6735`, `expand[]=customer` and `items[0][price]=price_1`. Parse such a body into nested dicts and lists, URL-decoding keys and values and rejecting conflicting shapes.
Every request to the Stripe API from curl or a form post goes through a decoder like this, so it is a realistic slice of the API layer and a good test of careful parsing. The interviewer checks a clean key tokenizer, correct handling of the three bracket forms, sensible conflict errors and a final pass that turns numeric indices into real lists.
What the Interviewer Expects
- Split pairs on `&` and the first `=`, then URL-decode with `+` meaning space
- Tokenize keys with a strict pattern and reject malformed brackets
- Treat `[]` as append, only allowed as the last segment
- Detect conflicts such as `a=1` combined with `a[b]=2` in either order
- Convert dicts whose keys are exactly `0..n-1` into lists in a post-processing pass
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you encode a nested object back into this format deterministically?
- How would you limit nesting depth and total keys to protect the API from abusive payloads?
- Should a sparse index like `items[0]` plus `items[2]` be an error? Why?
- How would you report errors with the exact parameter name for the API error response?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: a raw body string. Output: a dict. Leaves are strings (type coercion happens later against the endpoint's schema). Rules: plain key=value; nested a[b][c]=v; a[]=v appends; numeric segment...
High-Level Design
Two phases. Phase one builds a tree of dicts where every segment, including digits, is a string key, and [] targets a Python list. A lookahead on the next segment tells us whether to descend into a ...