Skip to content

Repository files navigation

CSV to JSON Converter Documentation

Overview

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.

Purpose

  • 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

How It Works

High-Level Process

  1. File List Setup: Define a list of CSV files to process
  2. Iterative Processing: Loop through each CSV file
  3. Data Reading: Read and parse the CSV content
  4. Structure Transformation: Convert rows to dictionary objects
  5. JSON Export: Save the transformed data as JSON files

Detailed Workflow

Step 1: File Input

csv_list = ['samplecsvfile.csv', 'samplecsvfile2.csv']
  • Maintains a list of CSV files to be converted
  • Easily extensible for additional files

Step 2: CSV Reading

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

Step 3: Header Extraction

headers = list_data[0]
  • Assumes the first row contains column headers
  • These headers become the keys in the resulting JSON objects

Step 4: Data Transformation

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

Step 5: JSON Output

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 .csv extension with .json
  • Saves the list of dictionaries as formatted JSON with 3-space indentation

Input/Output Example

Input CSV (sample.csv)

name,age,city,occupation
John Doe,28,New York,Engineer
Jane Smith,34,Los Angeles,Designer
Mike Johnson,42,Chicago,Manager

Output JSON (sample.json)

[
   {
      "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"
   }
]

Features

Error Handling

  • 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

User Feedback

  • Success messages for completed conversions
  • Clear error messages with specific file names
  • Progress indication during batch processing

Requirements

Python Modules

  • csv (built-in): For parsing CSV files
  • json (built-in): For creating JSON output

File Structure Assumptions

  • CSV files have headers in the first row
  • CSV files use standard comma separation
  • CSV files are properly formatted and readable

Usage Instructions

Basic Usage

  1. Setup: Place your CSV files in the same directory as the script
  2. Configuration: Update the csv_list variable with your CSV file names
  3. Execution: Run the script using python csv_to_json_converter.py
  4. Output: JSON files will be created in the same directory

Customization Options

  • File Locations: Modify file paths in csv_list for different directories
  • JSON Formatting: Adjust the indent parameter in json.dump() for different formatting
  • Error Handling: Extend exception handling for specific use cases

Limitations

Data Type Handling

  • 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

File Format Requirements

  • Assumes standard CSV format with comma separators
  • Requires consistent column structure across all rows
  • Header row must be present and correctly formatted

Potential Enhancements

Advanced Features

  • 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

Performance Improvements

  • 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

Troubleshooting

Common Issues

  1. "File not found" errors: Verify file paths and names in csv_list
  2. Empty JSON output: Check that CSV files contain data beyond headers
  3. Malformed JSON: Ensure CSV files are properly formatted without corrupted rows
  4. Permission errors: Verify read/write permissions for input and output directories

Best Practices

  • 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

About

Converts CSV files into a JSON file

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages