Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Flatten Nested Booking JSON With Python

HardPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Flatten Nested Booking JSON With Python. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Airbnb internal workflows may receive booking metadata as nested JSON. Given a decoded payload from an Airbnb booking workflow, transform each booking into one flat record with normalized text, numeric prices, and stay totals.

Requirements

  1. Process every object in payload["bookings"] and preserve input order.
  2. Trim leading and trailing whitespace from identifiers, collapse repeated whitespace in names and cities, lowercase statuses and emails, and title-case cities.
  3. Convert price strings such as "\$1,200.50" or " 30 " to Python floats.
  4. Use the first and last night dates as start_date and end_date. The nights are already in chronological order.
  5. Treat missing cleaning or service fees as 0.0.
  6. Return one flat dictionary per booking with these keys: booking_id, status, guest_name, guest_email, listing_id, city, start_date, end_date, nights, nightly_total, fees_total, and grand_total.

The function receives a Python dictionary, not a JSON string. Do not use third-party libraries.

Formal Specification

Input: payload, a dictionary containing a nonempty bookings list. Each booking contains the nested fields used below. Price values are strings, and each booking has at least one night.

Output: A list of flat dictionaries in the same order as the input bookings. Round no values; retain normal Python floating-point arithmetic.

Constraints

  • 1 <= len(payload["bookings"]) <= 10^4
  • 1 <= len(booking["stay"]["nights"]) <= 30
  • Each night contains an ISO date and a nonnegative price string
  • Fee fields, when present, contain nonnegative currency strings
  • Night records are already in chronological order

Function Signature

def flatten_booking_payload(payload):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output