jsonl.dumper¶
Low-level generator that yields formatted JSON Lines strings (or bytes) from an iterable of objects.
This is the building block used internally by jsonl.dump and jsonl.dumps.
Function Signature¶
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
iterable |
Iterable[Any] |
(required) | Iterable of JSON-serializable objects |
text_mode |
bool |
True |
If True, yield str lines; if False, yield bytes |
cls |
type[json.JSONEncoder] Callable or None |
json.JSONEncoder |
Custom encoder |
**kwargs |
Additional keyword arguments passed to the cls encoder |
Returns¶
Iterator[str | bytes] — A generator yielding one formatted line per object (including trailing newline).
Examples¶
Basic usage¶
import jsonl
data = [{"name": "Alice"}, {"name": "Bob"}]
for line in jsonl.dumper(data):
print(repr(line))
Output:
Yield bytes¶
import jsonl
data = [{"x": 1}, {"x": 2}]
for line in jsonl.dumper(data, text_mode=False):
print(repr(line))
Output:
Stream to a custom destination¶
import jsonl
data = [{"event": "click"}, {"event": "scroll"}]
# Write lines to stdout or any writable target
import sys
for line in jsonl.dumper(data):
sys.stdout.write(line)