Skip to content

Orbit2x/json-formatter-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 

Repository files navigation

JSON Formatter CLI & Online Tools - Complete Guide

JSON Formatter MIT License

Need to format JSON quickly? Try the free JSON Formatter at Orbit2x - beautify, minify, validate, and sort JSON instantly in your browser.

Quick Start - Format JSON Online

No installation needed! Use the free web tool:

👉 JSON Formatter at Orbit2x

Features:

  • ✅ Beautify JSON with 2/4 space indentation
  • ✅ Minify JSON for production
  • ✅ Validate JSON syntax with detailed errors
  • ✅ Sort keys alphabetically
  • ✅ Syntax highlighting (Dracula theme)
  • ✅ Real-time statistics (size, keys, depth)
  • ✅ Keyboard shortcuts (Ctrl+F to format)
  • ✅ Export formatted JSON
  • ✅ 100% client-side, privacy-focused

Command Line Tools

Using jq (Most Powerful)

# Install jq
brew install jq              # macOS
sudo apt-get install jq      # Ubuntu/Debian
choco install jq             # Windows

# Beautify JSON
cat input.json | jq '.'

# Minify JSON
cat input.json | jq -c '.'

# Sort keys alphabetically
cat input.json | jq -S '.'

# Format and save
jq '.' input.json > output.json

# Pretty print with 4-space indent
jq --indent 4 '.' input.json

# Extract specific fields
jq '.users[].name' data.json

# Filter arrays
jq '.[] | select(.age > 30)' users.json

Using python (Built-in)

# Beautify JSON (2-space indent)
python -m json.tool input.json

# Beautify with 4-space indent
python -m json.tool --indent 4 input.json

# Minify JSON
python -c "import json,sys;print(json.dumps(json.load(sys.stdin)))" < input.json

# Sort keys
python -c "import json,sys;print(json.dumps(json.load(sys.stdin),indent=2,sort_keys=True))" < input.json

Using node (JavaScript)

# Install globally
npm install -g json

# Beautify JSON
json < input.json

# Minify JSON
json -c < input.json

# Sort keys
json -o inspect < input.json

Custom Bash Function

Add to ~/.bashrc or ~/.zshrc:

# Format JSON from clipboard (macOS)
jsonformat() {
    pbpaste | jq '.' | pbcopy
    echo "✅ JSON formatted and copied to clipboard"
}

# Format JSON file
jsonfile() {
    if [ -z "$1" ]; then
        echo "Usage: jsonfile <input.json> [output.json]"
        return 1
    fi

    local input=$1
    local output=${2:-$1}

    jq '.' "$input" > /tmp/json_temp && mv /tmp/json_temp "$output"
    echo "✅ Formatted: $output"
}

# Validate JSON file
jsonvalidate() {
    if [ -z "$1" ]; then
        echo "Usage: jsonvalidate <file.json>"
        return 1
    fi

    if jq empty "$1" 2>/dev/null; then
        echo "✅ Valid JSON: $1"
    else
        echo "❌ Invalid JSON: $1"
        return 1
    fi
}

# Minify JSON file
jsonminify() {
    if [ -z "$1" ]; then
        echo "Usage: jsonminify <input.json> [output.json]"
        return 1
    fi

    local input=$1
    local output=${2:-$1}

    jq -c '.' "$input" > /tmp/json_temp && mv /tmp/json_temp "$output"
    echo "✅ Minified: $output"
}

Usage:

jsonformat                          # Format clipboard
jsonfile data.json                  # Format in place
jsonfile input.json output.json     # Format to new file
jsonvalidate data.json              # Validate JSON
jsonminify input.json output.min.json  # Minify

Programming Examples

JavaScript/Node.js

// Beautify JSON
const data = { name: "John", age: 30, city: "NYC" };
const beautified = JSON.stringify(data, null, 2);
console.log(beautified);
/*
{
  "name": "John",
  "age": 30,
  "city": "NYC"
}
*/

// Minify JSON
const minified = JSON.stringify(data);
console.log(minified);
// {"name":"John","age":30,"city":"NYC"}

// Sort keys alphabetically
const sorted = JSON.stringify(data, Object.keys(data).sort(), 2);

// Parse and format JSON string
const jsonString = '{"name":"John","age":30}';
const parsed = JSON.parse(jsonString);
const formatted = JSON.stringify(parsed, null, 2);

Python

import json

# Beautify JSON
data = {"name": "John", "age": 30, "city": "NYC"}
beautified = json.dumps(data, indent=2)
print(beautified)
"""
{
  "name": "John",
  "age": 30,
  "city": "NYC"
}
"""

# Minify JSON
minified = json.dumps(data, separators=(',', ':'))
print(minified)
# {"name":"John","age":30,"city":"NYC"}

# Sort keys
sorted_json = json.dumps(data, indent=2, sort_keys=True)

# Parse and format JSON file
with open('input.json', 'r') as f:
    data = json.load(f)

with open('output.json', 'w') as f:
    json.dump(data, f, indent=2, sort_keys=True)

Go

package main

import (
    "bytes"
    "encoding/json"
    "fmt"
    "log"
)

func main() {
    // Sample JSON
    jsonStr := `{"name":"John","age":30,"city":"NYC"}`

    // Beautify
    var prettyJSON bytes.Buffer
    err := json.Indent(&prettyJSON, []byte(jsonStr), "", "  ")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("Beautified:")
    fmt.Println(prettyJSON.String())

    // Minify
    var data interface{}
    json.Unmarshal([]byte(jsonStr), &data)
    minified, _ := json.Marshal(data)
    fmt.Println("\nMinified:")
    fmt.Println(string(minified))

    // Marshal with custom indent
    type Person struct {
        Name string `json:"name"`
        Age  int    `json:"age"`
        City string `json:"city"`
    }

    person := Person{Name: "John", Age: 30, City: "NYC"}
    formatted, _ := json.MarshalIndent(person, "", "    ")
    fmt.Println("\nFormatted struct:")
    fmt.Println(string(formatted))
}

PHP

<?php
// Beautify JSON
$data = [
    "name" => "John",
    "age" => 30,
    "city" => "NYC"
];

$beautified = json_encode($data, JSON_PRETTY_PRINT);
echo $beautified;
/*
{
    "name": "John",
    "age": 30,
    "city": "NYC"
}
*/

// Minify JSON
$minified = json_encode($data);
echo $minified;
// {"name":"John","age":30,"city":"NYC"}

// Sort keys
ksort($data);
$sorted = json_encode($data, JSON_PRETTY_PRINT);

// Parse and format JSON file
$jsonString = file_get_contents('input.json');
$data = json_decode($jsonString, true);
file_put_contents('output.json', json_encode($data, JSON_PRETTY_PRINT));
?>

Advanced Use Cases

1. Validate JSON Syntax

# Using jq
if jq empty input.json 2>/dev/null; then
    echo "✅ Valid JSON"
else
    echo "❌ Invalid JSON"
fi

# Using Python
python -c "import json,sys; json.load(open('input.json'))" && echo "✅ Valid" || echo "❌ Invalid"

# Using node
node -e "try { require('./input.json'); console.log('✅ Valid'); } catch(e) { console.log('❌ Invalid'); }"

Online: JSON Formatter shows detailed error messages

2. Extract Specific Fields

# Extract all names from array of users
jq '.users[].name' data.json

# Extract nested fields
jq '.company.employees[].email' data.json

# Multiple fields
jq '.users[] | {name: .name, age: .age}' data.json

3. Convert JSON to CSV

# Using jq
jq -r '.[] | [.name, .age, .city] | @csv' users.json > output.csv

# Using online tool
# 1. Format JSON: https://orbit2x.com/json-formatter
# 2. Convert to CSV: https://orbit2x.com/converter

4. Compare Two JSON Files

# Using diff
diff <(jq -S '.' file1.json) <(jq -S '.' file2.json)

# Using node
node -e "
const f1 = require('./file1.json');
const f2 = require('./file2.json');
console.log(JSON.stringify(f1) === JSON.stringify(f2) ? '✅ Same' : '❌ Different');
"

5. Merge Multiple JSON Files

# Using jq
jq -s '.[0] * .[1]' file1.json file2.json > merged.json

# Merge array of objects
jq -s 'add' *.json > combined.json

6. Remove Comments from JSON5

// JSON5 allows comments - need to strip for standard JSON
const JSON5 = require('json5');
const fs = require('fs');

const json5Content = fs.readFileSync('config.json5', 'utf8');
const parsed = JSON5.parse(json5Content);
const standardJSON = JSON.stringify(parsed, null, 2);
fs.writeFileSync('config.json', standardJSON);

Common JSON Errors & Fixes

Error 1: Trailing Comma

// ❌ Invalid
{
  "name": "John",
  "age": 30,  // <-- Trailing comma
}

// ✅ Valid
{
  "name": "John",
  "age": 30
}

Error 2: Single Quotes

// ❌ Invalid
{
  'name': 'John'  // <-- Single quotes
}

// ✅ Valid
{
  "name": "John"
}

Error 3: Unquoted Keys

// ❌ Invalid
{
  name: "John"  // <-- Unquoted key
}

// ✅ Valid
{
  "name": "John"
}

Error 4: Comments

// ❌ Invalid
{
  // This is a comment
  "name": "John"
}

// ✅ Valid
{
  "name": "John"
}

Error 5: Undefined/Function Values

// ❌ Invalid JSON
const obj = {
  name: "John",
  greet: function() { return "Hello"; },  // Functions not allowed
  data: undefined  // undefined not allowed
};

// ✅ Valid JSON
const obj = {
  name: "John",
  greet: "Hello",
  data: null  // Use null instead of undefined
};

Detailed error messages: Use JSON Formatter for line-by-line error detection


JSON Best Practices

1. Use Consistent Indentation

// ✅ 2-space indent (recommended for web)
{
  "user": {
    "name": "John"
  }
}

// ✅ 4-space indent (recommended for code)
{
    "user": {
        "name": "John"
    }
}

2. Sort Keys Alphabetically

// ✅ Easier to find keys
{
  "age": 30,
  "city": "NYC",
  "name": "John"
}

3. Use Meaningful Key Names

// ❌ Bad
{
  "a": "John",
  "b": 30
}

// ✅ Good
{
  "name": "John",
  "age": 30
}

4. Keep Arrays Homogeneous

// ❌ Mixed types
{
  "data": [1, "two", { "three": 3 }]
}

// ✅ Consistent types
{
  "numbers": [1, 2, 3],
  "strings": ["one", "two", "three"],
  "objects": [{ "id": 1 }, { "id": 2 }]
}

5. Minify for Production, Beautify for Development

# Development
jq '.' api-response.json > dev.json

# Production
jq -c '.' api-response.json > prod.json

Performance Tips

Minification Savings

Original Size Minified Size Savings
100 KB 75 KB 25%
1 MB 700 KB 30%
10 MB 7 MB 30%

Rule of thumb: Minification saves 25-35% file size

When to Minify

  • ✅ API responses in production
  • ✅ Configuration files in Docker images
  • ✅ Static JSON served over CDN
  • ❌ Development files (keep readable)
  • ❌ JSON stored in databases (DB compresses)

gzip Compression

# Minify + gzip for maximum savings
jq -c '.' large.json | gzip > compressed.json.gz

# Result: 60-80% smaller than original

Tools & Resources

Online Tools (No Installation)

CLI Tools

  • jq - Swiss Army knife for JSON
  • json - Node.js JSON tool
  • fx - Interactive JSON viewer

Libraries

  • JavaScript: Native JSON.stringify() and JSON.parse()
  • Python: Built-in json module
  • Go: encoding/json package
  • PHP: json_encode() and json_decode()
  • Rust: serde_json
  • Java: Jackson

References


FAQ

Q: What's the difference between JSON and JSON5?

A: JSON5 allows comments, trailing commas, unquoted keys. Standard JSON does not. Use JSON Formatter to convert JSON5 → JSON.

Q: How do I format JSON in VSCode?

A:

  1. Select JSON text
  2. Press Shift+Alt+F (Windows/Linux) or Shift+Option+F (macOS)
  3. Or use Command Palette → "Format Document"

Q: Can I format JSON from API responses?

A: Yes!

# cURL + jq
curl https://api.example.com/users | jq '.'

# Or use online: https://orbit2x.com/json-formatter

Q: What's the maximum JSON file size I can format?

A:

  • CLI tools: Unlimited (limited by RAM)
  • Online tools: Most support up to 10MB
  • Orbit2x JSON Formatter: Handles 50MB+ files (client-side processing)

Q: How do I fix "Unexpected token" errors?

A: Use JSON Formatter - it shows exact line/column of errors with suggestions.


Related Tools

JSON Tools

Developer Tools


Contributing

Found a better way to format JSON? Have a useful script? Open a PR!

License

MIT License - Free to use in your projects


Made with ❤️ by Orbit2x - Free Developer Tools

Try it now: JSON FormatterAll Tools

About

JSON Formatter CLI & Online Tools - Complete Guide

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published