Process a JSON-Lines Stream

Medium ⏱ 12 min 63% acceptance ★★★★☆ 4.4
Big exports use JSON-Lines: one JSON object per line. Write count_events(text, kind) that parses each non-empty line separately and counts objects whose 'event' equals kind, skipping lines that fail to parse.

Examples

Example 1
Input
count_events(text_with_three_jsonl_lines, "click")  # lines: click, view, click
Output
2
Explanation

Each line is its own document.

Constraints

  • Parse line by line — the whole text is NOT valid JSON.
  • Bad lines are skipped.

Topics

JSONJSONL

Companies

OpenSearchElasticDatabricks

Hints

Hint 1

text.splitlines() then loads per line.

Hint 2

A try/except inside the loop.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.