Implementation:AGI Edgerunners LLM Agents Papers write json
| Knowledge Sources | |
|---|---|
| Domains | Data_I/O, Serialization |
| Last Updated | 2026-10-10 14:30 GMT |
Overview
Concrete utility for serializing Python dicts to JSON files, provided by the LLM-Agents-Papers repository.
Description
The write_json function opens a file for writing (creating or truncating), serializes a Python dict to JSON with ensure_ascii=False (preserving Unicode) and indent=4 (pretty-printed), and closes the file. It is defined in script_v5_step1.py and script_v5_step2.py. The function is used to persist parsed paper metadata to parsed_v5/<year>.json files, both at the end of the crawl loop and as an emergency save in the exception handler.
Usage
Import this function when you need to persist structured data as a JSON file:
- Saving parsed paper metadata after crawling (parsed_v5/<year>.json)
- Emergency save on script interruption (exception handler in parse_paper)
- Writing any dict to a pretty-printed UTF-8 JSON file
Code Reference
Source Location
- Repository: LLM-Agents-Papers
- Files: script_v5_step1.py:L37-40, script_v5_step2.py:L36-39
Signature
def write_json(file: str, data: dict) -> None:
f = open(file, 'w+', encoding='utf-8')
json.dump(data, f, ensure_ascii=False, indent=4)
f.close()
Import
# Each script defines its own local copy; no shared import exists.
# In script_v5_step1.py:
from script_v5_step1 import write_json
# In script_v5_step2.py:
from script_v5_step2 import write_json
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| file | str | Yes | Output path for the JSON file |
| data | dict | Yes | Python dict to serialize |
Outputs
| Name | Type | Description |
|---|---|---|
| return | None | No return value; writes file to disk |
| file | File on disk | UTF-8 encoded, indented JSON file at file path |
Usage Examples
Persisting Parsed Paper Metadata
from hashlib import md5
# Build parsed paper data
url = "https://arxiv.org/abs/2310.01444v2"
md5_id = md5(url.encode()).hexdigest()
parsed = {
"title": "Example Paper Title",
"authors": ["Author One", "Author Two"],
"date": "2024/01/15",
"pdf": "https://arxiv.org/pdf/2310.01444v2",
"abstract": "Example abstract...",
"code": "",
"category": [["Survey", "Planning"]],
"url": url
}
# Save to year-keyed JSON file
stored_datas = {"2024": {md5_id: parsed}}
for year, datas in stored_datas.items():
write_json(f"parsed_v5/{year}.json", datas)
print(f"Saved {len(datas)} papers to parsed_v5/{year}.json")
Emergency Save on Interruption
# Pattern used in parse_paper()'s exception handler:
# If crawling is interrupted, save whatever was collected so far
try:
# ... crawling logic ...
pass
except Exception as e:
for year, datas in stored_datas.items():
write_json(f"parsed_v5/{year}.json", datas)
print(f"Saved data on error: {e}")