A simple, lightweight Python utility to convert JSON files to CSV format.
JSON2CSV is a command-line tool that makes it easy to convert JSON data into CSV format. It handles both single JSON objects and arrays of objects, automatically detecting all fields and creating appropriate CSV headers.
- Simple to Use: Just provide the input JSON file and optionally specify an output CSV file
- Automatic Field Detection: Automatically extracts all unique keys from JSON objects to create CSV headers
- Flexible Input: Handles both single JSON objects and arrays of objects
- Smart Output Naming: Automatically generates output filename based on input if not specified
- Error Handling: Comprehensive error messages for common issues (file not found, invalid JSON, etc.)
- UTF-8 Support: Full Unicode support for international characters
- Sorted Headers: CSV headers are alphabetically sorted for consistency
- Clone this repository:
git clone https://github.com/jayeshjain2025-lgtm/new-repo.git
cd new-repo- No additional dependencies required - uses only Python standard library!
Convert a JSON file to CSV (output will be named automatically):
python JSON2CSV.py input.jsonConvert JSON to CSV with a custom output filename:
python JSON2CSV.py input.json output.csvIf you have a JSON file data.json:
[
{"name": "Alice", "age": 30, "city": "New York"},
{"name": "Bob", "age": 25, "city": "San Francisco"},
{"name": "Charlie", "age": 35, "city": "Chicago"}
]Running:
python JSON2CSV.py data.jsonWill create data.csv:
age,city,name
30,New York,Alice
25,San Francisco,Bob
35,Chicago,Charlie
- Python 3.6 or higher
- No external dependencies
You can also import and use the conversion function in your own Python scripts:
from JSON2CSV import json_to_csv
# Convert a JSON file to CSV
success = json_to_csv('input.json', 'output.csv')
if success:
print("Conversion successful!")
else:
print("Conversion failed")The tool provides clear error messages for common issues:
- File not found
- Invalid JSON format
- Empty JSON files
- No valid data structure
MIT License - feel free to use this tool in your projects!
Contributions are welcome! Feel free to:
- Report bugs
- Suggest new features
- Submit pull requests
Created by jayeshjain2025-lgtm
If you encounter any issues or have questions, please open an issue on GitHub.