Flatten Nested Reels Watch Event Payloads with json_normalize
Last updated: November 29, 2026
Quick Overview
Raw client events arrive as JSON with nested objects, e.g. {'event_id', 'user': {'id', 'country'}, 'media': {'reel_id', 'duration_ms'}, 'watch': {'ms', 'muted'}}, some with missing keys. Flatten them into a typed DataFrame and compute per-reel average watch fraction, ignoring events without a duration.
Meta
November 29, 20260
0
0 solved
Raw client events arrive as JSON with nested objects, e.g. {'event_id', 'user': {'id', 'country'}, 'media': {'reel_id', 'duration_ms'}, 'watch': {'ms', 'muted'}}, some with missing keys. Flatten them into a typed DataFrame and compute per-reel average watch fraction, ignoring events without a duration.
Data engineers at Meta spend real time turning semi-structured client logs into clean tables, so screens include a small JSON-to-DataFrame task. The interviewer checks json_normalize usage, defensive handling of missing nested fields, type coercion and capping fractions at one.
What the Interviewer Expects
- Use pd.json_normalize with a separator to flatten nested keys
- Handle records that are missing whole sub-objects
- Coerce numeric fields and treat zero or missing duration as invalid
- Clip watch fraction to [0, 1] because looping can exceed duration
- Aggregate per reel with counts so the average has context
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you handle a field whose type changes between app versions (string versus int)?
- What schema-evolution strategy would you use if this fed a Hive table?
- How would you flatten a nested list, like an array of ad impressions inside each event?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Input is a list of dicts. Some events lack media.duration_ms or the whole watch object. Watch time can exceed duration because Reels loop, so the fraction should be capped at 1. I will output `ree...
Approach
pd.json_normalize(events, sep='_') turns nested keys into columns like media_reel_id and fills missing keys with NaN. Then pd.to_numeric(errors='coerce') guards against strings, I drop rows with...