Skip to content

mrvi0/cli-json-formatter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

JSON Formatter & Validator CLI

License: MIT Shell: Bash Dependency: jq Platform: Linux Version: 1.0.0

πŸš€ Lightweight and fast CLI tool for JSON validation and formatting.

✨ Features

  • βœ… JSON Validation - check JSON syntax correctness
  • 🎨 Pretty Print - beautiful formatting with customizable indentation
  • πŸ“ File Operations - read from files and save results
  • πŸ”§ Flexible Options - customize indentation, show additional information
  • 🎯 Error Handling - clear error messages
  • πŸ“Š JSON Information - statistics and metadata
  • ⚑ Fast Startup - single bash script, no npm dependencies

πŸ› οΈ Installation

Installation Platform Dependencies

Quick Installation (Recommended)

# Clone the repository
git clone https://github.com/mrvi0/cli-json-fomatter.git
cd json-formatter-cli

# Install to system
sudo ./install.sh

Manual Installation

# Install jq (if not installed)
sudo apt install jq  # Ubuntu/Debian
sudo yum install jq  # CentOS/RHEL
sudo pacman -S jq    # Arch Linux

# Make script executable
chmod +x jsonfmt

# Copy to system directory
sudo cp jsonfmt /usr/local/bin/

πŸš€ Usage

Usage Features Output

Basic Usage

# Validate and format JSON string
jsonfmt '{"name": "John", "age": 30}'

# Work with file
jsonfmt -f examples/sample.json

# Read from stdin
echo '{"data": "value"}' | jsonfmt

Command Line Options

# Save result to file
jsonfmt -f input.json -o output.json

# Customize indentation (default: 2 spaces)
jsonfmt -f input.json -i 4

# Show additional information
jsonfmt -f input.json --info

# Full help
jsonfmt --help

Usage Examples

1. Validate JSON File

jsonfmt -f examples/sample.json

Output:

βœ… JSON is valid (file: examples/sample.json)

πŸ“‹ Formatted JSON:
{
  "name": "JSON Formatter CLI",
  "version": "1.0.0",
  "description": "A command-line tool for JSON validation and formatting",
  ...
}

2. Format with Information

jsonfmt -f examples/sample.json --info

Output:

βœ… JSON is valid (file: examples/sample.json)

πŸ“‹ Formatted JSON:
{...}

πŸ“Š JSON Information:
   Root element type: Object
   Size: 456 characters
   Number of properties: 6

3. Save to File

jsonfmt -f examples/sample.json -o formatted.json

Output:

βœ… JSON is valid (file: examples/sample.json)
πŸ“ Formatted JSON saved to: formatted.json

4. Error Handling

jsonfmt -f invalid.json

Output:

❌ Invalid JSON (file: invalid.json):
   parse error: Unexpected token } at line 1, column 15

πŸ§ͺ Testing

Tests Coverage Status

Run tests to verify functionality:

chmod +x test.sh
./test.sh

Tests verify:

  • Valid JSON validation
  • Invalid JSON handling
  • JSON string formatting
  • File saving
  • Indentation settings
  • Help output

πŸ“ Project Structure

json-formatter-cli/
β”œβ”€β”€ jsonfmt              # Main bash script
β”œβ”€β”€ install.sh           # Installation script
β”œβ”€β”€ uninstall.sh         # Uninstall script
β”œβ”€β”€ test.sh              # Tests
β”œβ”€β”€ examples/
β”‚   └── sample.json      # Sample JSON file
β”œβ”€β”€ README.md           # Documentation
β”œβ”€β”€ LICENSE             # MIT license
└── .gitignore          # Ignored files

πŸ”§ Technical Details

Dependencies

  • jq - JSON processor for validation and formatting
  • bash 4.0+ - command line interpreter

Main Functions

  • validate_json() - JSON syntax validation
  • format_json() - indentation formatting
  • read_file() - safe file reading
  • write_file() - safe file writing
  • get_json_info() - JSON metadata extraction

Error Handling

  • JSON syntax validation via jq
  • File existence and permission checks
  • Read/write error handling
  • Clear error messages with colored output

🎯 Bash Version Advantages

Fast Startup Minimal Dependencies Lightweight Linux Native

  • ⚑ Fast Startup - no npm install required
  • 🎯 Minimal Dependencies - only jq
  • πŸ”§ Simple Installation - single installation script
  • πŸ“¦ Lightweight - single file ~5KB
  • 🐧 Native Linux Support - works out of the box
  • 🎨 Colored Output - beautiful result display

🎯 Project Usage

This CLI tool is perfect for:

  • Validating JSON configuration files
  • Formatting API responses
  • Data validation in CI/CD pipelines
  • JSON processing in automation scripts
  • Quick JSON checking in command line

🀝 Contributing

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

πŸ‘¨β€πŸ’» Author

Vi - GitHub

πŸ“ž Support


⭐ If you like this project, give it a star on GitHub!

About

Lightweight CLI tool for JSON validation and formatting. Fast bash script with jq dependency

Topics

Resources

License

Stars

Watchers

Forks

Languages