Your question is Extract Filtered Events from JSON. 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.
Acme Analytics stores application logs as a large JSON payload. Write a function that parses the payload and extracts only the events that match a target event type and optional attribute filters.
Given a JSON string representing an object with an events array, return a list of simplified event objects for every valid event whose type matches target_type and whose key-value pairs in required_attrs are all present inside the event's attributes object.
Implement:
payload: a JSON stringtarget_type: a stringrequired_attrs: a dictionary of required attribute filtersReturn a list of dictionaries. Each output dictionary must contain:
id: event idtimestamp: event timestampuser_id: user id if present, otherwise nullIgnore malformed event entries, events missing required top-level fields (id, type, timestamp), and events whose attributes field is not an object.
id, type, and timestamprequired_attrs may be empty, in which case all events of the target type qualifydef extract_events(payload, target_type, required_attrs):