Python: Read JSON Line by Line

Language:
Python
6 views
0 favorites
2 hours ago

Code Implementation

Python
import json

def read_jsonl(file_path):
    with open(file_path, "r", encoding="utf-8") as f:
        for line in f:
            yield json.loads(line)

# Usage
for record in read_jsonl("data.jsonl"):
    print(record)

Read JSONL (JSON Lines) files line by line in Python, suitable for processing large-scale datasets.

#json#jsonl

Snippet Description

  • Each line in a JSONL file is an independent JSON object
  • Use yield to implement a line-by-line generator to avoid loading all data at once
  • Often used in log and big data scenarios

Comments

Loading...