Notes fanzru's shorts
Date 12 / 22 / 2024
E = mc²
∇²Ψ + V(x)Ψ = EΨ
∫f(x)dx
Back to Shorts
jsonterminalformattingtools

Quick JSON Format

1,245 views

Format JSON file

bash
# Pretty print JSON
cat file.json | jq '.'

# Format and save
jq '.' file.json > formatted.json

# Minify JSON
jq -c '.' file.json > minified.json

Validate JSON

bash
# Check if valid
jq empty < file.json && echo "Valid JSON" || echo "Invalid JSON"

# Or with Python
python -m json.tool file.json

Quick JSON from terminal

bash
# Create JSON from command output
echo '{"name": "John", "age": 30}' | jq '.'

# Extract specific field
echo '{"users": [{"name": "John"}]}' | jq '.users[0].name'

Install jq (if not installed)

bash
# Mac
brew install jq

# Ubuntu/Debian
sudo apt install jq

Pro tip: Add alias json="jq '.'" to your aliases! 🚀