Categories We Write About

Batch convert CSVs to JSON

To batch convert CSV files to JSON, you can use a Python script. Below is a script that reads all .csv files in a folder and converts each one into a .json file with the same name.

python
import os import csv import json # Set your input and output folder paths input_folder = 'csv_files' # Folder containing CSV files output_folder = 'json_files' # Folder where JSON files will be saved # Create output folder if it doesn't exist os.makedirs(output_folder, exist_ok=True) # Process each CSV file in the input folder for filename in os.listdir(input_folder): if filename.endswith('.csv'): csv_path = os.path.join(input_folder, filename) json_path = os.path.join(output_folder, filename.replace('.csv', '.json')) with open(csv_path, mode='r', encoding='utf-8') as csv_file: reader = csv.DictReader(csv_file) data = list(reader) with open(json_path, mode='w', encoding='utf-8') as json_file: json.dump(data, json_file, indent=4) print("Batch conversion complete.")

Instructions:

  1. Save the script to a .py file.

  2. Create a folder named csv_files and place your .csv files inside.

  3. Run the script. It will create a json_files folder and store the converted JSON files there.

Would you like a version that merges all CSVs into one JSON file instead?

Share This Page:

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories We Write About