JSON to CSV Converter - thepoopooman652/proishs-webtools GitHub Wiki
JSON to CSV Converter - Complete Documentation
Overview
The JSON to CSV Converter is a web-based tool built with PHP that allows users to upload JSON (JavaScript Object Notation) files and convert them to CSV (Comma-Separated Values) format. The tool provides a clean, user-friendly interface with real-time conversion and instant download capabilities.
Features
Core Functionality
-
File Upload
- Accepts JSON files through a drag-and-drop style interface
- File type validation to ensure only JSON files are processed
- Visual feedback showing the selected file name
-
JSON to CSV Conversion
- Parses JSON arrays of objects
- Extracts headers from the first object's keys
- Converts each object to a CSV row
- Handles special characters with proper CSV escaping
- Converts data types appropriately for CSV format
-
File Download
- One-click download of converted CSV file
- Automatic filename generation (replaces .json extension with .csv)
- Browser-based download without server-side file storage
-
Error Handling
- File upload validation
- JSON syntax verification
- Data structure validation (must be array of objects)
- Element-level validation (all items must be objects)
- Clear error messages for users
How It Works
Technical Architecture
The application is built as a single-page PHP application that combines:
- HTML for structure
- CSS for styling
- JavaScript for client-side interactions
- PHP for server-side JSON processing
Workflow Process
-
User Uploads JSON File
- User clicks "Choose JSON File" button
- Browser file picker opens with .json filter
- Selected file name is displayed on the interface
- "Convert to CSV" button becomes enabled
-
Conversion Process
- User clicks "Convert to CSV" button
- JavaScript prevents default form submission
- File is sent to server via AJAX POST request
- PHP processes the file on the server
-
Server-Side Processing
- PHP validates the uploaded file
- Checks file extension is .json
- Reads and parses the JSON content
- Validates JSON syntax using
json_decode() - Checks that data is a non-empty array
- Validates that all elements are objects/arrays
-
Data Validation The converter performs multiple validation checks:
- JSON syntax: Must be valid JSON format
- Array structure: Must be an array, not a single object
- Non-empty: Array must contain at least one element
- Object elements: Each array element must be an object
- Element consistency: All elements validated individually
-
CSV Generation
- Headers extracted from first object's keys
- Each object converted to a CSV row
- Data types handled appropriately:
- Booleans: Converted to "true" or "false" strings
- Null values: Converted to empty strings
- Nested objects/arrays: Serialized as JSON strings
- Other values: Converted to strings
-
Download
- CSV data array sent back to browser
- JavaScript creates properly formatted CSV string
- Special characters escaped (commas, quotes, newlines)
- Blob object created from CSV string
- Browser downloads file with appropriate name
File Structure
index.php
The main application file contains:
HTML Structure
- Modern, responsive container layout
- File upload form with styled input
- Upload area with visual feedback
- Convert and download buttons
- Information section with usage instructions
CSS Styling
- Gradient background for visual appeal
- Card-based container design
- Interactive hover effects
- Responsive design for mobile devices
- Color-coded success/error messages
- Disabled button states for better UX
JavaScript Functionality
- File input change detection
- Form submission handling via AJAX
- Dynamic UI updates (messages, button states)
- CSV formatting with proper escaping
- CSV blob creation and download
- Automatic filename generation
PHP Backend
- POST request handling
- File upload validation
- JSON parsing and validation
- Element-level validation loop
- CSV data array generation
- HTTP response with JSON data
Usage Instructions
For End Users
-
Open the Application
- Navigate to index.php in your web browser
- You'll see the JSON to CSV Converter interface
-
Upload Your JSON File
- Click the "Choose JSON File" button
- Select a JSON file from your computer
- The file name will appear below the button
-
Convert the File
- Click the "Convert to CSV" button
- Wait for the conversion (usually instant)
- Success message will appear with record count
-
Download the Result
- Click the "Download CSV File" button
- The CSV file will download to your computer
- File will have the same name as your JSON but with .csv extension
JSON File Requirements
Your JSON file should be formatted as follows:
- Structure: Array of objects (not a single object)
- Non-empty: Must contain at least one object
- Consistent format: All elements should be objects
- Encoding: UTF-8 recommended for special characters
Example JSON:
[
{
"name": "John Doe",
"age": 30,
"email": "[email protected]",
"active": true
},
{
"name": "Jane Smith",
"age": 25,
"email": "[email protected]",
"active": false
},
{
"name": "Bob Johnson",
"age": 35,
"email": "[email protected]",
"active": true
}
]
Resulting CSV:
name,age,email,active
John Doe,30,[email protected],true
Jane Smith,25,[email protected],false
Bob Johnson,35,[email protected],true
Technical Details
PHP Functions Used
-
file_get_contents()- Reads entire file into a string
- Used to get JSON content from uploaded file
- Returns false on failure
-
json_decode()- Converts JSON string to PHP array/object
- Second parameter
trueconverts to associative array - Sets error code accessible via
json_last_error()
-
json_last_error()- Returns last JSON parsing error code
- Used to validate JSON syntax
JSON_ERROR_NONEindicates successful parsing
-
json_encode()- Converts PHP array/object to JSON string
- Used for:
- Creating API responses
- Serializing nested objects in CSV cells
-
array_keys()- Extracts keys from associative array
- Used to get CSV headers from first object
- Returns array of key names
-
pathinfo()- Gets file extension for validation
- Ensures only .json files are processed
Data Type Conversion Logic
The converter uses the following rules for CSV generation:
// Boolean conversion
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
// Null conversion
elseif (is_null($value)) {
$value = '';
}
// Nested objects/arrays
elseif (is_array($value) || is_object($value)) {
$value = json_encode($value);
}
// All other types remain as-is
CSV Escaping Logic
JavaScript handles CSV escaping client-side:
row.map(cell => {
const cellStr = String(cell);
// Escape if contains comma, quote, or newline
if (cellStr.includes(',') || cellStr.includes('"') || cellStr.includes('\n')) {
// Wrap in quotes and escape existing quotes
return '"' + cellStr.replace(/"/g, '""') + '"';
}
return cellStr;
}).join(',')
Rules:
- Commas: Field wrapped in double quotes
- Quotes: Doubled ("" for each ")
- Newlines: Field wrapped in double quotes
- Normal text: No escaping needed
Security Features
-
File Type Validation
- Only .json extensions accepted
- Server-side validation in addition to client-side
-
Upload Error Checking
- Verifies file was uploaded successfully
- Checks for PHP upload errors
-
JSON Validation
- Syntax validation via
json_decode() - Structure validation (array check)
- Element-level validation (all items are objects)
- Syntax validation via
-
File Size Limits
- Controlled by PHP configuration
- Default limits apply from php.ini
-
No Server-Side Storage
- Files processed in memory
- Temporary files cleaned up automatically
- No persistent storage of user data
Browser Compatibility
The application works with modern browsers that support:
- HTML5 File API
- Fetch API
- Blob and URL.createObjectURL
- ES6 JavaScript features (arrow functions, template literals)
Compatible browsers include:
- Chrome 60+
- Firefox 55+
- Safari 11+
- Edge 79+
Error Messages
The application provides clear error messages for common issues:
| Error | Cause | Solution |
|---|---|---|
| "No file uploaded or upload error occurred" | File upload failed | Try uploading again |
| "Only JSON files are allowed" | Wrong file type selected | Select a .json file |
| "Unable to read the JSON file" | File is corrupted or unreadable | Check file integrity |
| "Invalid JSON format: [error]" | Malformed JSON syntax | Validate JSON syntax |
| "JSON must be a non-empty array of objects" | Wrong JSON structure | Use array format |
| "Invalid data at index N: all elements must be objects/arrays" | Mixed data types in array | Ensure all elements are objects |
Customization Options
Changing CSV Delimiter
The default delimiter is comma (,). To use a different delimiter (e.g., semicolon):
// Change this line in the download handler
.join(',')
// To this (for semicolon delimiter)
.join(';')
Modifying CSV Escaping
To change escaping behavior, modify the download handler logic:
// Current: Escape commas, quotes, and newlines
if (cellStr.includes(',') || cellStr.includes('"') || cellStr.includes('\n'))
// Example: Only escape quotes
if (cellStr.includes('"'))
Styling Customization
The CSS is embedded in the HTML. Key variables you might want to change:
- Primary color:
#667eea(purple-blue gradient start) - Secondary color:
#764ba2(purple gradient end) - Container width:
max-width: 600px - Border radius:
border-radius: 12px
Limitations
-
File Size
- Limited by PHP's
upload_max_filesizesetting - Default is typically 2MB-8MB depending on server
- Large files may timeout or run out of memory
- Limited by PHP's
-
JSON Structure
- Must be an array of objects
- All array elements must be objects/arrays
- Headers taken from first object only
-
Browser Storage
- CSV stored temporarily in browser memory
- Very large conversions may cause memory issues
- Recommended for files under 10MB
-
Header Consistency
- Headers based on first object's keys
- If objects have different keys, some cells may be empty
- Missing keys filled with empty values
Troubleshooting
File Won't Upload
- Check file extension is .json
- Verify file isn't corrupted
- Try with a smaller file
- Check browser console for errors
Conversion Fails
- Validate JSON syntax using an online validator
- Ensure JSON is an array, not a single object
- Check that all array elements are objects
- Look for special characters causing issues
Download Doesn't Work
- Enable pop-ups for the site
- Check browser's download settings
- Ensure JavaScript is enabled
- Try a different browser
Empty or Incorrect CSV
- Verify JSON structure matches expected format
- Check that objects have consistent keys
- Ensure no nested arrays at the top level
- Validate data types in JSON
Advanced Usage
Nested Objects
If your JSON contains nested objects, they will be serialized as JSON strings in the CSV:
Input JSON:
[
{
"name": "John",
"address": {
"city": "NYC",
"zip": "10001"
}
}
]
Output CSV:
name,address
John,"{""city"":""NYC"",""zip"":""10001""}"
Boolean and Null Values
- Booleans: Converted to literal strings "true" and "false"
- Null: Converted to empty strings in CSV
- This ensures compatibility with most CSV parsers
Array Values
Arrays within objects are serialized as JSON strings:
Input:
[
{
"name": "John",
"hobbies": ["reading", "coding"]
}
]
Output:
name,hobbies
John,"[""reading"",""coding""]"
Conclusion
The JSON to CSV Converter is a reliable tool for transforming JSON data into CSV format. It handles the most common JSON to CSV conversion needs while maintaining data integrity and providing a smooth user experience. The single-file architecture makes it easy to deploy on any PHP-enabled web server without additional dependencies or database requirements.