Batch Format and Validate JSON Files
Pretty-print, minify, and validate hundreds of JSON files in one command. Perfect for CI/CD pipelines, config management, and API fixture formatting.
The Problem
Your project has dozens of JSON config files, API fixtures, or i18n translation files that are inconsistently formatted. Some are minified, some have tabs, some have trailing commas. You need them all consistently formatted.
Why Batch Processing Matters
Consistent JSON formatting improves code review readability, reduces merge conflicts, and catches syntax errors before deployment. Running a formatter in CI/CD ensures every JSON file meets the same standard — just like ESLint or Prettier for JavaScript.
Common Use Cases
- Format all JSON fixtures in a test suite for readable diffs
- Validate JSON configs before deployment in CI/CD pipeline
- Minify JSON API responses for production (reduce payload size)
- Normalize i18n translation files across multiple locales
Step-by-Step Instructions
Find all JSON files
Use find or glob to locate JSON files. Exclude node_modules, .git, and lock files to avoid processing generated files.
Validate syntax first
Check for syntax errors before formatting. Invalid JSON will cause the formatter to fail — catch these early and fix them.
Format in-place or to output directory
Use jq . for pretty-printing or jq -c . for minifying. Python's json.tool module also works without installing anything.
Verify and commit
Run git diff to review changes. If formatting looks correct, commit with a message like "chore: format JSON files".
Code Examples
# Pretty-print all JSON files in-place (using jq)
find . -name '*.json' -not -path '*/node_modules/*' -not -name 'package-lock.json' | \
while read f; do
jq . "$f" > "$f.tmp" && mv "$f.tmp" "$f" && echo "Formatted: $f"
done
# Validate all JSON files (check syntax only)
find . -name '*.json' -not -path '*/node_modules/*' | \
while read f; do
python3 -m json.tool "$f" > /dev/null 2>&1 || echo "INVALID: $f"
done
# Minify all JSON files (remove whitespace)
find ./api/fixtures -name '*.json' -exec sh -c '
jq -c . "$1" > "$1.min" && mv "$1.min" "$1"
' _ {} \;
# Sort keys consistently across all files
find . -name '*.json' -exec sh -c 'jq -S . "$1" > "$1.tmp" && mv "$1.tmp" "$1"' _ {} \;
Single vs Batch Comparison
Paste JSON → click Format → copy pretty-printed output
$ python format_json.py Formatted 87 JSON files Errors (2): ./config/broken.json: Expecting ',' delimiter: line 5 column 3 ./fixtures/incomplete.json: Unterminated string starting at: line 12
Download Workflow Template
Save this JSON workflow template to automate this process in your CI/CD pipeline or scripts.
Download batch-json-format.jsonFrequently Asked Questions
Should I format JSON files in CI/CD?
Yes. Add find . -name '*.json' | xargs python3 -m json.tool > /dev/null as a CI step to catch syntax errors. For formatting enforcement, use a pre-commit hook with jq or a JSON linter.
Does formatting change the data?
No. Formatting only changes whitespace and key ordering. The parsed data is identical. One exception: if your JSON has duplicate keys (invalid per spec), some formatters will keep only the last value.
What's the best indent size for JSON?
2 spaces is the most common convention (used by Prettier, VS Code defaults, and most APIs). 4 spaces is also popular. Tabs are rare in JSON. Pick one and enforce it project-wide.
Related Batch Guides
Try these tools interactively
Each tool runs in your browser with no signup required. Process single items instantly.