| Crates.io | json-formatter-cli |
| lib.rs | json-formatter-cli |
| version | 0.1.0 |
| created_at | 2026-01-09 10:47:13.083919+00 |
| updated_at | 2026-01-09 10:47:13.083919+00 |
| description | A fast JSON formatter written in Rust |
| homepage | |
| repository | https://github.com/mwanjajoel/rust-json-formatter |
| max_upload_size | |
| id | 2031919 |
| size | 17,611 |
A fast and reliable command-line tool written in Rust for formatting, validating, and pretty-printing JSON files.
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 .
Format a JSON file:
json-formatter input.json
This will read input.json, validate it, and print the formatted JSON to stdout.
Pipe JSON content:
cat input.json | json-formatter
# or
echo '{"key":"value"}' | json-formatter
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
When developing or testing, you can use cargo run instead of building the binary first:
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
# Minify JSON (compact output)
cargo run -- data/nested.json --minify
# Pipe JSON content
cat data/simple.json | cargo run --
# Echo JSON and format
echo '{"key":"value","number":42}' | cargo run --
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"
]
}
# Using the binary
json-formatter data.json --minify
# Or using cargo run
cargo run -- data.json --minify
curl -s https://api.example.com/data.json | json-formatter
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
The tool provides clear error messages for common issues:
Error: File 'filename.json' not foundError: Invalid JSON: <detailed error message>Error: Permission denied: <file path>Error: Failed to read file: <error details>Exit codes:
0 - Success1 - General error (invalid JSON, file error, etc.)cargo build
The debug binary will be at target/debug/json-formatter
cargo build --release
The optimized binary will be at target/release/json-formatter
cargo test
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.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)This project is licensed under the MIT License - see the LICENSE file for details.
Issue: command not found: json-formatter
Issue: Permission denied
Issue: Invalid JSON error
JSON Formatter is built with Rust for optimal performance:
Benchmark results (on typical JSON files):
Future enhancements may include:
-o, --output)-i, --indent)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.