DEV Community

Devadatta Baireddy
Devadatta Baireddy

Posted on

JSON Formatter CLI - Format, Validate, and Analyze JSON in Seconds

JSON Formatter CLI — Format, Validate, and Analyze JSON in Seconds

You get API responses. They're minified. Unreadable.

You have config files. Keys are unsorted. Inconsistent.

You're debugging. You need to validate JSON structure.

Stop copying to online tools. Stop installing npm packages.

I built a zero-dependency JSON formatter that does it all in one command.

The Problem

Developers work with JSON constantly. But:

  • API responses are minified (hard to read)
  • Config files are inconsistent (multiple formats)
  • Validation errors don't show the structure
  • Online tools are slow and privacy-invasive
  • npm packages require 100+ dependencies

You need a simple, local, fast solution.

The Solution

python json_formatter.py data.json
Enter fullscreen mode Exit fullscreen mode

Pretty-printed, readable JSON. Takes 10ms.

python json_formatter.py data.json --sort
Enter fullscreen mode Exit fullscreen mode

All keys alphabetically sorted. Clean.

python json_formatter.py data.json --minify
Enter fullscreen mode Exit fullscreen mode

Minified for production. 40% smaller.

python json_formatter.py data.json --stats
Enter fullscreen mode Exit fullscreen mode

Understand the structure instantly.

Key Features

✅ Multiple Formats

Pretty Print (readable, development)

python json_formatter.py data.json
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "name": "John",
  "age": 30,
  "city": "NYC"
}
Enter fullscreen mode Exit fullscreen mode

Minified (compact, production)

python json_formatter.py data.json --minify
Enter fullscreen mode Exit fullscreen mode

Output: {"name":"John","age":30,"city":"NYC"}

Sorted (consistent, for version control)

python json_formatter.py data.json --sort
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "age": 30,
  "city": "NYC",
  "name": "John"
}
Enter fullscreen mode Exit fullscreen mode

Compact (balanced, for sharing)

python json_formatter.py data.json --compact
Enter fullscreen mode Exit fullscreen mode

✅ Validation & Analysis

Validate

python json_formatter.py data.json --validate
Enter fullscreen mode Exit fullscreen mode

Output: ✓ Valid JSON

Statistics

python json_formatter.py data.json --stats
Enter fullscreen mode Exit fullscreen mode

Output:

JSON Statistics:
  Structure: Object
  Objects: 15
  Arrays: 8
  Strings: 32
  Numbers: 12
  Top keys: name, email, id, ...
Enter fullscreen mode Exit fullscreen mode

✅ Batch Processing

Process multiple files:

for f in data/*.json; do
    python json_formatter.py "$f" --sort -o "$f"
done
Enter fullscreen mode Exit fullscreen mode

✅ Production Ready

  • Zero dependencies
  • < 100ms for most files
  • Handles deeply nested structures
  • UTF-8 encoding support

Real-World Examples

Example 1: Debug API Response

Your API endpoint returns minified JSON. Debug it:

curl https://api.example.com/users | python json_formatter.py /dev/stdin
Enter fullscreen mode Exit fullscreen mode

Before:

{"status":"success","data":{"users":[{"id":1,"name":"Alice","email":"alice@example.com"},{"id":2,"name":"Bob","email":"bob@example.com"}]},"timestamp":"2024-01-15T10:30:00Z"}
Enter fullscreen mode Exit fullscreen mode

After:

{
  "status": "success",
  "data": {
    "users": [
      {
        "id": 1,
        "name": "Alice",
        "email": "alice@example.com"
      },
      {
        "id": 2,
        "name": "Bob",
        "email": "bob@example.com"
      }
    ]
  },
  "timestamp": "2024-01-15T10:30:00Z"
}
Enter fullscreen mode Exit fullscreen mode

Now you can see the structure instantly.

Example 2: Standardize Config Files

Your Kubernetes config files have inconsistent formatting:

# Standardize all configs
for config in *.json; do
    python json_formatter.py "$config" --sort -o "$config"
done

git add *.json
git commit -m "Standardize JSON format"
Enter fullscreen mode Exit fullscreen mode

Benefits:

  • Consistent formatting across team
  • Easier diffs in version control
  • No merge conflicts from formatting

Example 3: Data Pipeline Optimization

Processing large JSON files:

# Read pretty version (development)
python json_formatter.py raw-data.json

# Process and convert
python processor.py raw-data.json

# Output minified (production)
python json_formatter.py output.json --minify -o api-response.json
Enter fullscreen mode Exit fullscreen mode

Saves 40-60% bandwidth on APIs!

Example 4: Quick Validation

Before importing to database, validate JSON:

python json_formatter.py import.json --validate

# Batch validate
for f in imports/*.json; do
    python json_formatter.py "$f" --validate || echo "Invalid: $f"
done
Enter fullscreen mode Exit fullscreen mode

Performance Comparison

Tool Speed Dependency Setup
This tool 10-50ms None 2 min
jq 50-100ms Binary 10 min
npm (prettier) 200ms+ 100+ packages 5 min
Online tools 1000ms+ Cloud 1 min
Python json lib Varies Requires Python script 5 min

Winner: This tool for 95% of use cases.

How It Works

Simple Python architecture:

class JSONFormatter:
    def format_pretty(data, indent=2):
        return json.dumps(data, indent=indent, sort_keys=False)

    def format_minified(data):
        return json.dumps(data, separators=(',', ':'))

    def get_stats(data):
        # Count objects, arrays, strings, etc.
        # Identify top keys
        return statistics
Enter fullscreen mode Exit fullscreen mode

No complex logic. Just Python's built-in json module with smart wrapping.

Installation

Get it free on GitHub:
👉 github.com/devdattareddy/json-formatter-cli

git clone https://github.com/devdattareddy/json-formatter-cli
cd json-formatter-cli

# Run
python json_formatter.py data.json
Enter fullscreen mode Exit fullscreen mode

No installation. No pip. Just run it.

Use Cases

🔧 API Development - Debug responses

🗄️ DevOps - Validate configs

📊 Data Engineering - Process JSON pipelines

🌐 Web Development - Format data files

🔍 Debugging - Understand structure

Common Workflows

API Debugging Workflow

# Get API response
curl https://api.example.com/data > response.json

# Format it
python json_formatter.py response.json

# Validate structure
python json_formatter.py response.json --stats

# Save pretty version
python json_formatter.py response.json -o pretty.json
Enter fullscreen mode Exit fullscreen mode

Config File Workflow

# Check config
python json_formatter.py app-config.json --validate

# Standardize
python json_formatter.py app-config.json --sort -o app-config.json

# Deploy minified
python json_formatter.py app-config.json --minify -o app-config-prod.json
Enter fullscreen mode Exit fullscreen mode

Data Analysis Workflow

# Analyze raw data
python json_formatter.py raw-data.json --stats

# Format for review
python json_formatter.py raw-data.json > formatted.json

# Process
python processor.py formatted.json

# Minify for export
python json_formatter.py output.json --minify -o output-min.json
Enter fullscreen mode Exit fullscreen mode

Why I Built This

I was debugging API responses by copying to online formatters. Every response. Slow. Insecure.

Then I was standardizing JSON config files manually. Tedious.

Finally I built this: 200-line Python script that does all three.

Saves 2-3 hours per week.

Get Started

# Clone
git clone https://github.com/devdattareddy/json-formatter-cli

# Try it
echo '{"name":"John","age":30}' > test.json
python json_formatter.py test.json
Enter fullscreen mode Exit fullscreen mode

Support This Project

If this tool saves you time:

🎉 Buy Me a Coffee - Help me build more tools

Star on GitHub - Help others find it


What's your go-to JSON formatting tool? Let me know — I might add features you need!

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.