Automation
Beginner
25 min
Building Your First Code Snippet
Create, document, and publish a production-quality automation script to the community library.
by Karol Szekely · Apr 08, 2026
Building Your First Code Snippet
Ready to contribute to the community? This tutorial walks you through creating a professional automation script from scratch.
Prerequisites
- Completed the Getting Started tutorial
- Python 3.8+ with a virtual environment
- Understanding of basic Python (functions, file I/O)
Step 1: Identify a Task to Automate
Look for repetitive tasks in your daily workflow:
- Renaming files in bulk
- Converting between data formats
- Sending templated emails
- Processing spreadsheets
- Scraping data from websites
For this tutorial, we'll build a JSON to CSV converter.
Step 2: Write Clean Code
import json
import csv
from pathlib import Path
from typing import Optional
def json_to_csv(
input_file: str,
output_file: Optional[str] = None,
delimiter: str = ",",
) -> dict:
"""
Convert a JSON array file to CSV format.
Args:
input_file: Path to the JSON file (must contain a list of objects).
output_file: Output CSV path. Defaults to same name with .csv extension.
delimiter: CSV delimiter character.
Returns:
Dict with conversion stats.
Raises:
ValueError: If JSON doesn't contain a list of objects.
"""
input_path = Path(input_file)
if not input_path.exists():
raise FileNotFoundError(f"File not found: {input_file}")
with open(input_path, encoding="utf-8") as f:
data = json.load(f)
if not isinstance(data, list) or not data:
raise ValueError("JSON must contain a non-empty array of objects")
if output_file is None:
output_file = str(input_path.with_suffix(".csv"))
headers = list(data[0].keys())
with open(output_file, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=headers, delimiter=delimiter)
writer.writeheader()
writer.writerows(data)
return {
"input": input_file,
"output": output_file,
"rows": len(data),
"columns": len(headers),
}
Step 3: Add Error Handling
Always handle edge cases:
- File not found
- Invalid JSON format
- Empty data arrays
- Encoding issues
- Permission errors
Step 4: Test Thoroughly
if __name__ == "__main__":
# Test with sample data
result = json_to_csv("sample_data.json")
print(f"Converted: {result['rows']} rows, {result['columns']} columns")
print(f"Output: {result['output']}")
Step 5: Publish to the Library
- Navigate to your Dashboard
- Click New Snippet
- Fill in: title, description, language, category
- Paste your code
- Submit for community review
What You Learned
- How to structure an automation script with proper typing and docstrings
- How to handle errors gracefully
- How to test your script before publishing
- How to contribute to the vAIbecode library