json-formatter-cli

Crates.iojson-formatter-cli
lib.rsjson-formatter-cli
version0.1.0
created_at2026-01-09 10:47:13.083919+00
updated_at2026-01-09 10:47:13.083919+00
description A fast JSON formatter written in Rust
homepage
repositoryhttps://github.com/mwanjajoel/rust-json-formatter
max_upload_size
id2031919
size17,611
mwanjajoel (mwanjajoel)

documentation

README

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

  1. Ensure you have Rust installed (version 1.70 or later)

  2. Clone the repository:

    git clone <repository-url>
    cd json-formatter
    
  3. Build the project:

    cargo build --release
    
  4. The binary will be available at target/release/json-formatter

  5. (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 - Success
  • 1 - 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.

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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.

Commit count: 7

cargo fmt