
Given a file path to a large log file where each line is a JSON object, write a Python function that scans the file line by line, extracts values for the key error_code, and returns a dictionary mapping each error code to its frequency. Ignore lines that are invalid JSON or valid JSON objects that do not contain error_code.
Example 1:
Input file lines:
{"timestamp":"2024-01-01T10:00:00Z","level":"ERROR","error_code":"E101"}
{"timestamp":"2024-01-01T10:01:00Z","level":"INFO"}
{"timestamp":"2024-01-01T10:02:00Z","level":"ERROR","error_code":"E101"}
{"timestamp":"2024-01-01T10:03:00Z","level":"ERROR","error_code":"E205"}
Output: {"E101": 2, "E205": 1}
Explanation: Only lines containing `error_code` are counted.
Example 2:
Input file lines:
{"error_code":500}
not-json
{"error_code":500}
{"message":"ok"}
Output: {500: 2}
Explanation: Invalid JSON and missing `error_code` entries are skipped.
1 <= number of lines <= 10^7error_code values may be strings or integersGiven a file path to a large log file where each line is a JSON object, write a Python function that scans the file line by line, extracts values for the key error_code, and returns a dictionary mapping each error code to its frequency. Ignore lines that are invalid JSON or valid JSON objects that do not contain error_code.
Example 1:
Input file lines:
{"timestamp":"2024-01-01T10:00:00Z","level":"ERROR","error_code":"E101"}
{"timestamp":"2024-01-01T10:01:00Z","level":"INFO"}
{"timestamp":"2024-01-01T10:02:00Z","level":"ERROR","error_code":"E101"}
{"timestamp":"2024-01-01T10:03:00Z","level":"ERROR","error_code":"E205"}
Output: {"E101": 2, "E205": 1}
Explanation: Only lines containing `error_code` are counted.
Example 2:
Input file lines:
{"error_code":500}
not-json
{"error_code":500}
{"message":"ok"}
Output: {500: 2}
Explanation: Invalid JSON and missing `error_code` entries are skipped.
1 <= number of lines <= 10^7error_code values may be strings or integers