This Python script converts CSV (Comma-Separated Values) files to JSON (JavaScript Object Notation) format. It processes multiple CSV files in batch and transforms each row into a JSON object where column headers become keys and cell values become the corresponding values.
- Data Format Conversion: Convert tabular CSV data into structured JSON format
- Batch Processing: Handle multiple CSV files in a single execution
- Data Integration: Prepare CSV data for web applications, APIs, or systems that consume JSON
- Data Analysis: Transform CSV data into a format more suitable for programmatic processing
- File List Setup: Define a list of CSV files to process
- Iterative Processing: Loop through each CSV file
- Data Reading: Read and parse the CSV content
- Structure Transformation: Convert rows to dictionary objects
- JSON Export: Save the transformed data as JSON files
csv_list = ['samplecsvfile.csv', 'samplecsvfile2.csv']- Maintains a list of CSV files to be converted
- Easily extensible for additional files
with open(csv_file, mode='r') as file:
csv_data_object = csv.reader(file)
list_data = list(csv_data_object)- Opens each CSV file in read mode
- Uses Python's built-in
csv.reader()for proper CSV parsing - Converts the reader object to a list for easier manipulation
headers = list_data[0]- Assumes the first row contains column headers
- These headers become the keys in the resulting JSON objects
for row in list_data[1:]:
row_dict = dict(zip(headers, row))
dict_list.append(row_dict)- Processes each data row (skipping the header row)
- Uses
zip()to pair each header with corresponding cell values - Creates a dictionary for each row
- Accumulates all row dictionaries in a list
json_file_path = csv_file.replace('.csv', '.json')
with open(json_file_path, mode='w') as json_file:
json.dump(dict_list, json_file, indent=3)- Generates output filename by replacing
.csvextension with.json - Saves the list of dictionaries as formatted JSON with 3-space indentation
name,age,city,occupation
John Doe,28,New York,Engineer
Jane Smith,34,Los Angeles,Designer
Mike Johnson,42,Chicago,Manager[
{
"name": "John Doe",
"age": "28",
"city": "New York",
"occupation": "Engineer"
},
{
"name": "Jane Smith",
"age": "34",
"city": "Los Angeles",
"occupation": "Designer"
},
{
"name": "Mike Johnson",
"age": "42",
"city": "Chicago",
"occupation": "Manager"
}
]- File Not Found: Gracefully handles missing CSV files
- Empty Files: Checks for and skips empty CSV files
- General Exceptions: Catches and reports unexpected errors
- Continued Processing: Errors with one file don't stop processing of remaining files
- Success messages for completed conversions
- Clear error messages with specific file names
- Progress indication during batch processing
csv(built-in): For parsing CSV filesjson(built-in): For creating JSON output
- CSV files have headers in the first row
- CSV files use standard comma separation
- CSV files are properly formatted and readable
- Setup: Place your CSV files in the same directory as the script
- Configuration: Update the
csv_listvariable with your CSV file names - Execution: Run the script using
python csv_to_json_converter.py - Output: JSON files will be created in the same directory
- File Locations: Modify file paths in
csv_listfor different directories - JSON Formatting: Adjust the
indentparameter injson.dump()for different formatting - Error Handling: Extend exception handling for specific use cases
- All CSV values are treated as strings in the JSON output
- No automatic type conversion (numbers, booleans, dates)
- Special characters in CSV data are preserved as-is
- Assumes standard CSV format with comma separators
- Requires consistent column structure across all rows
- Header row must be present and correctly formatted
- Data Type Detection: Automatically convert numeric and boolean values
- Custom Delimiters: Support for semicolon, tab, or other separators
- Nested JSON: Create hierarchical JSON structures from relational data
- Configuration Files: Use external config files for batch processing settings
- Validation: Add data validation and quality checks
- Compression: Support for compressed CSV/JSON files
- Streaming Processing: Handle large files without loading entirely into memory
- Parallel Processing: Process multiple files simultaneously
- Progress Bars: Visual progress indication for large batch operations
- "File not found" errors: Verify file paths and names in
csv_list - Empty JSON output: Check that CSV files contain data beyond headers
- Malformed JSON: Ensure CSV files are properly formatted without corrupted rows
- Permission errors: Verify read/write permissions for input and output directories
- Test with small sample files first
- Backup original CSV files before processing
- Validate JSON output with online JSON validators
- Use descriptive file names for easy identification