| name | json-output |
| description | Generate structured JSON output files with proper formatting. Use when creating JSON reports, exporting aggregated data, formatting API responses, or saving processed results to files. |
JSON Output
Provides patterns for generating JSON output.
Writing JSON Files
import json
from pathlib import Path
from typing import Any
from datetime import datetime
def write_json(filepath: str, data: Any, indent: int = 2):
"""Write data to JSON file with formatting."""
Path(filepath).parent.mkdir(parents=True, exist_ok=True)
def json_serializer(obj):
if isinstance(obj, datetime):
return obj.isoformat()
raise TypeError(f"Object of type {type(obj)} is not JSON serializable")
with open(filepath, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=indent, default=json_serializer, ensure_ascii=False)
Report Generation
from datetime import datetime
from typing import List, Dict, Any
def create_aggregation_report(
results: List[Dict],
sources: List[str],
query: str
) -> Dict[str, Any]:
"""Create a structured aggregation report."""
return {
"report_type": "content_aggregation",
"generated_at": datetime.now().isoformat(),
"query": query,
"sources": sources,
"summary": {
"total_results": len(results),
"unique_domains": len(set(
result.get("url", "").split("/")[2] if "://" in result.get("url", "") else ""
for result in results
)),
"avg_score": sum(r.get("computed_score", 0) for r in results) / len(results) if results else 0
},
"results": results
}
Structured Output
def format_result(result: Dict, include_content: bool = True) -> Dict:
"""Format a single result for output."""
formatted = {
"url": result.get("url"),
"title": result.get("title"),
"description": result.get("description"),
"score": round(result.get("computed_score", 0), 3),
"source": result.get("source"),
"metadata": {
"author": result.get("author"),
"published_date": result.get("published_date"),
"domain": result.get("url", "").split("/")[2] if "://" in result.get("url", "") else ""
}
}
if include_content:
formatted["content_preview"] = result.get("content", "")[:500]
return formatted
def format_results(results: List[Dict], include_content: bool = True) -> List[Dict]:
"""Format all results for output."""
return [format_result(r, include_content) for r in results]