A Python utility that automatically converts all tables from a SQLite database to JSON format.
This tool provides a simple command-line interface to export all tables from a SQLite database file (.db
) to JSON format. It can generate individual JSON files for each table or combine all tables into a single JSON file.
- Export all tables automatically
- Option for pretty (indented) JSON output
- Save each table as a separate JSON file or combine all into one file
- Preserves column names as JSON property names
- Handles any SQLite database structure
- Python 3.6+
- No external dependencies (uses only standard library modules)
Clone or download this repository, or just save the script as sqlite_to_json.py
.
# Make the script executable (for Linux/Mac)
chmod +x sqlite_to_json.py
Basic usage:
python sqlite_to_json.py database.db
With options:
python sqlite_to_json.py database.db --output-dir ./exported_data --pretty --single-file
Argument | Short | Description |
---|---|---|
db_path |
Path to the SQLite database file (required) | |
--output-dir |
-o |
Directory to save JSON files (default: ./json_output) |
--pretty |
-p |
Generate pretty (indented) JSON |
--single-file |
-s |
Save all tables to a single JSON file |
The script:
- Connects to the specified SQLite database
- Retrieves a list of all tables
- For each table:
- Fetches all data and column names
- Converts rows to JSON-compatible dictionaries
- Preserves column name -> value relationships
- Saves the data to the specified output directory
- Either as separate files named after each table
- Or as a single file with all tables
When using separate files, each table will be saved as [table_name].json
:
json_output/
├── users.json
├── products.json
└── orders.json
Sample content of a JSON file:
[
{
"id": 1,
"name": "John Doe",
"email": "[email protected]"
},
{
"id": 2,
"name": "Jane Smith",
"email": "[email protected]"
}
]
When using the --single-file
option, all tables will be in all_tables.json
:
{
"users": [
{
"id": 1,
"name": "John Doe",
"email": "[email protected]"
}
],
"products": [
{
"id": 101,
"name": "Laptop",
"price": 999.99
}
]
}
- Data migration between systems
- Creating backups in a human-readable format
- Exporting data for web applications
- Analyzing database structure and content
- Sharing data with systems that consume JSON
- Large databases may require significant memory during processing
- Binary data in the SQLite database may not convert properly to JSON
- Table names with special characters might cause issues