1.5 KiB
1.5 KiB
JSON Formatter Utility
File: json_formatter.py
Location: /root/.hermes/wizard-outputs/spark/json_formatter.py
Purpose
A Python utility that validates and pretty-prints JSON files with customizable indentation.
Usage
python3 json_formatter.py <input.json> [indent]
Arguments:
input.json(required): Path to the JSON file to formatindent(optional): Number of spaces for indentation (default: 2)
Features
- Validates JSON syntax before formatting
- Sort keys alphabetically for consistent output
- Handles Unicode characters properly (ensure_ascii=False)
- Descriptive error messages with line/column for invalid JSON
- Exit codes: 0 (success), 1 (no args), 2 (file not found), 3 (JSON error), 4 (other error)
Verification
Test 1: Valid JSON file
$ python3 json_formatter.py /root/.hermes/gateway_state.json
Result: Successfully formatted valid JSON file with 96 lines output.
Test 2: Invalid JSON handling
$ python3 json_formatter.py test_invalid.json
JSON Error: Expecting property name enclosed in double quotes at line 1, column 2
Exit code: 3
Result: Correctly detected and reported JSON syntax error.
Test 3: Output validation
$ python3 -m json.tool gateway_state_formatted.json
Result: JSON is valid - confirms output is valid JSON.
Artifact Location
- Script:
/root/.hermes/wizard-outputs/spark/json_formatter.py - Formatted output:
/root/.hermes/wizard-outputs/spark/gateway_state_formatted.json