jsonl.dumps¶
Serialize an iterable of objects into a JSON Lines formatted string.
Function Signature¶
Parameters¶
| Parameter | Type | Default | Description |
|---|---|---|---|
iterable |
Iterable[Any] |
(required) | Iterable of objects to serialize |
cls |
type[json.JSONEncoder] Callable or None |
json.JSONEncoder |
Custom encoder |
**kwargs |
Additional keyword arguments passed to the cls encoder |
Returns¶
str — A string with one JSON object per line.
Examples¶
Basic usage¶
Output:
With custom serialization¶
import jsonl
data = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]
# Compact output
result = jsonl.dumps(data, separators=(",", ":"))
print(result)
Output:
With sorted keys¶
import jsonl
data = [{"z": 1, "a": 2}, {"m": 3, "b": 4}]
result = jsonl.dumps(data, sort_keys=True)
print(result)
Output: