1 unstable release
Uses new Rust 2024
| 0.1.0 | Jan 9, 2026 |
|---|
#55 in Value formatting
10KB
59 lines
JSON Formatter
A fast and reliable command-line tool written in Rust for formatting, validating, and pretty-printing JSON files.
Features
- 📄 Read JSON files - Read JSON from files or standard input
- ✅ Validate JSON - Ensure JSON syntax is correct before formatting
- 🎨 Pretty print - Format JSON with consistent indentation and spacing
- 🚀 Fast performance - Built with Rust for optimal speed
- 🔧 Flexible input - Support for file paths and stdin
- 📤 Flexible output - Output formatted JSON to stdout
- 🛡️ Error handling - Clear error messages for invalid JSON
Installation
From Source
-
Ensure you have Rust installed (version 1.70 or later)
-
Clone the repository:
git clone <repository-url> cd json-formatter -
Build the project:
cargo build --release -
The binary will be available at
target/release/json-formatter -
(Optional) Install globally:
cargo install --path .
Usage
Basic Usage
Format a JSON file:
json-formatter input.json
This will read input.json, validate it, and print the formatted JSON to stdout.
Read from Standard Input
Pipe JSON content:
cat input.json | json-formatter
# or
echo '{"key":"value"}' | json-formatter
Command-Line Options
USAGE:
json-formatter [OPTIONS] [FILE]
ARGS:
<FILE> Input JSON file (if not provided, reads from stdin)
OPTIONS:
--minify Minify the JSON output (compact format)
-h, --help Print help information
-V, --version Print version information
Development Usage
When developing or testing, you can use cargo run instead of building the binary first:
Format Sample JSON Files
The project includes sample JSON files in the data/ folder for testing:
# Format simple.json
cargo run -- data/simple.json
# Format nested.json
cargo run -- data/nested.json
# Format complex.json
cargo run -- data/complex.json
Format with Options
# Minify JSON (compact output)
cargo run -- data/nested.json --minify
Read from stdin
# Pipe JSON content
cat data/simple.json | cargo run --
# Echo JSON and format
echo '{"key":"value","number":42}' | cargo run --
Examples
Example 1: Format a JSON file
Input file (data.json):
{"name":"John Doe","age":30,"city":"New York","hobbies":["reading","coding","traveling"]}
Command:
# Using the binary
json-formatter data.json
# Or using cargo run (for development)
cargo run -- data.json
Output:
{
"name": "John Doe",
"age": 30,
"city": "New York",
"hobbies": [
"reading",
"coding",
"traveling"
]
}
Example 2: Minify JSON output
# Using the binary
json-formatter data.json --minify
# Or using cargo run
cargo run -- data.json --minify
Example 3: Pipe JSON from another command
curl -s https://api.example.com/data.json | json-formatter
Example 4: Validate JSON without formatting
The tool will automatically validate JSON and exit with an error code if the JSON is invalid:
json-formatter invalid.json
# Error: Invalid JSON: expected value at line 1 column 1
Error Handling
The tool provides clear error messages for common issues:
- File not found:
Error: File 'filename.json' not found - Invalid JSON:
Error: Invalid JSON: <detailed error message> - Permission denied:
Error: Permission denied: <file path> - Read error:
Error: Failed to read file: <error details>
Exit codes:
0- Success1- General error (invalid JSON, file error, etc.)
Building
Development Build
cargo build
The debug binary will be at target/debug/json-formatter
Release Build
cargo build --release
The optimized binary will be at target/release/json-formatter
Running Tests
cargo test
Dependencies
- clap - Command-line argument parsing
- serde - Serialization framework
- serde_json - JSON serialization/deserialization
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Troubleshooting
Common Issues
Issue: command not found: json-formatter
- Solution: Make sure the binary is in your PATH or use the full path to the binary
Issue: Permission denied
- Solution: Ensure you have read permissions for input files and write permissions for output files
Issue: Invalid JSON error
- Solution: Verify your JSON file is valid. You can use online JSON validators to check the syntax
Performance
JSON Formatter is built with Rust for optimal performance:
- Fast parsing and serialization
- Low memory footprint
- Efficient file I/O operations
Benchmark results (on typical JSON files):
- Small files (< 1KB): < 1ms
- Medium files (1KB - 100KB): < 10ms
- Large files (100KB - 1MB): < 100ms
Roadmap
Future enhancements may include:
- Support for JSON5 format
- Output to file option (
-o,--output) - Custom indentation size (
-i,--indent) - Custom sorting of object keys
- Colorized output for terminals
- Batch processing of multiple files
- JSON schema validation
- Support for JSON streaming
Author
Created with ❤️ using Rust
Note: This tool is designed to be simple, fast, and reliable. It focuses on the core functionality of formatting and validating JSON without unnecessary complexity.
Dependencies
~1.3–2.4MB
~45K SLoC