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.
Example 1
Input: payload = '{"events":[{"id":"e1","type":"click","timestamp":100,"user":{"id":"u1"},"attributes":{"page":"home"}},{"id":"e2","type":"view","timestamp":101,"attributes":{"page":"home"}}]}', target_type = "click", required_attrs = {"page":"home"}
Output: [{"id":"e1","timestamp":100,"user_id":"u1"}]
Example 2
Input: payload = '{"events":[{"id":"e1","type":"click","timestamp":100,"attributes":{"page":"home","device":"mobile"}},{"id":"e2","type":"click","timestamp":101,"attributes":{"page":"home"}}]}', target_type = "click", required_attrs = {"page":"home","device":"mobile"}
Output: [{"id":"e1","timestamp":100,"user_id":null}]
1 <= number of events <= 10^5