This project provides a Python script (csv-to-json.py
) that converts CSV files to JSON format. It is particularly useful for processing large datasets and handling missing values by leaving corresponding JSON values blank.
- The script assumes the first row of the CSV file contains column headers, which are used as keys in the JSON objects.
- Each subsequent row in the CSV file is converted into a JSON object.
- The script removes any columns and rows that are completely empty.
- It is designed to work with standard CSV files that have a clear header row and consistent columns.
Example JSON output:
[
{"Rank":1.0, "Authors":"Jon Doe", ... },
{"Rank":2.0, "Authors":"Jane Doe", ... },
{"Rank":3.0, "Authors":"George Orwell", ... },
{"Rank":4.0, "Authors":"Julia Donaldson", ... }
]
- Python 3.x
- Pandas library
First, clone the repository to your local machine:
git clone https://github.com/your-username/convert-csv-to-json.git
Ensure you have Python 3 installed. If not, download and install it from the Python official website.
It's recommended to create a virtual environment for your Python projects. Here's how you can do it:
python3 -m venv myenv
source myenv/bin/activate
python -m venv myenv
myenv\Scripts\activate
With your virtual environment activated, install the required packages using:
pip install -r requirements.txt
Run the script with the following command:
python csv-to-json.py <source_csv_path> [destination_json_path]
<source_csv_path>
: The path to the source CSV file.[destination_json_path]
: Optional. The path where the JSON file will be saved. If not specified, the JSON file will be saved in the same directory as the source file, with the same base name.
-
Specifying both source and destination:
python csv-to-json.py path/to/source.csv path/to/destination.json
-
Specifying only the source (the JSON file will be saved in the same location as the CSV file):
python csv-to-json.py path/to/source.csv
This project is open-sourced under the MIT license.