llmscope.link
Developer GuidesPublished 2026-04-107 min read

How to Format, Validate, and Repair Broken JSON (Terminal, Python, and Browser Guide)

Fix messy or broken JSON payloads quickly using jq, Python one-liners, and client-side browser tools. Complete guide on resolving trailing commas, unquoted keys, and 64-bit integer bugs.

K
Khalid ARCore Team
Principal Systems Architect
Zero Trackers • Pure Client-Side Content

The 2 AM Production Log Nightmare

Every engineer who has touched backend microservices knows this exact moment: a critical production alert fires at 2 AM. You pull an error payload from AWS CloudWatch, Datadog, or a Stripe webhook, and it lands in your clipboard as a single, unreadable 4-megabyte wall of minified text.

Worse still, you paste it into your editor or hit your API handler, and you get hit with: SyntaxError: Unexpected token , in JSON at position 4819. You cannot format it because the syntax is broken, and you cannot easily spot where the syntax broke because it is unformatted.

Understanding how JSON parsers behave under the hood—and knowing the exact terminal commands and local tools to clean it up—saves hours of blind debugging.

Important Notice

Never paste production JSON containing customer PII, Stripe secret keys, or authentication tokens into random public web formatters. Many third-party utilities record request payloads into server access logs.

Formatting in the Terminal: The Zero-Friction Methods

If you work on Linux, macOS, or WSL, your first line of defense should always be the terminal. It is instantaneous, handles gigabyte-sized files, and keeps your data strictly local.

The standard industry tool is jq. To format a minified JSON file with standard 2-space indentation, run:

bash
READ-ONLY CODE SAMPLE
# Basic pretty-print with 2-space indentation
jq . input.json > formatted.json

# Pretty-print from clipboard and pipe directly back (macOS)
pbpaste | jq . | pbcopy

# Pretty-print from clipboard in PowerShell (Windows)
Get-Clipboard | jq . | Set-Clipboard
// Pretty-printing JSON using jq CLI

Formatting with Python Standard Library (No Installs Required)

If you are on an air-gapped jump host or minimal Docker container without jq installed, Python is almost always available out of the box. Python ships with a built-in JSON module that acts as a standalone CLI tool:

bash
READ-ONLY CODE SAMPLE
# Format file in-place or output to stdout
python3 -m json.tool input.json formatted.json

# Format raw string from curl response
curl -s https://api.example.com/v1/health | python3 -m json.tool
// Using Python json.tool module

The Four Syntax Traps That Break RFC 8259

Unlike JavaScript object literals, JSON has a very rigid specification defined in RFC 8259. The four most common reasons standard parsers fail include:

1. Trailing Commas: Modern ECMAScript allows trailing commas in objects and arrays. In JSON, {"name": "Alice", "role": "admin",} will crash any compliant parser.

2. Single Quotes: Keys and string values MUST be wrapped in double quotes ("). Single quotes ('name') are completely illegal in JSON.

3. Unquoted Keys: When developers log JavaScript objects using console.log({ status: 200 }), the terminal prints unquoted keys. A JSON parser will reject this immediately.

4. Comments: JSON does not permit // single-line or /* */ multi-line comments. If you need comments, your file is JSONC (JSON with Comments), which requires specialized parsers.

Pro Tip

If you are dealing with JavaScript object notation instead of strict JSON, use a client-side validator that automatically sanitizes unquoted keys and trailing commas before passing them to JSON.parse().

The Silent Killer: 64-Bit Integer Truncation

Here is a subtle bug that has caused real financial discrepancies: JavaScript numbers are IEEE 754 double-precision floating-point numbers. The maximum safe integer is Number.MAX_SAFE_INTEGER (9,007,199,254,740,991 or 2^53 - 1).

If your database uses 64-bit integer primary keys (like Twitter Snowflake IDs or PostgreSQL BIGINTs such as 1849204928104829104), passing this number unquoted through a standard JSON.parse() call will silently round the last two digits to 1849204928104829000.

Always serialize 64-bit integers as strings in JSON payloads over HTTP to prevent catastrophic ID corruption.

javascript
READ-ONLY CODE SAMPLE
// Notice how the ID silently mutates when parsed!
const payload = '{"order_id": 1849204928104829104}';
const parsed = JSON.parse(payload);
console.log(parsed.order_id); // 1849204928104829200 (CORRUPTED!)

// The correct approach: quote 64-bit IDs
const safePayload = '{"order_id": "1849204928104829104"}';
// Demonstration of 64-bit integer overflow in JavaScript JSON.parse

Zero-Upload Browser Formatting with LLMScope

When you need to validate, repair, and beautify JSON with visual tree navigation, line-number error highlights, and zero setup, LLMScope JSON Formatter runs entirely inside your browser tab.

The parser executes inside a sandboxed Web Worker. No text ever leaves your device RAM, guaranteeing complete privacy for confidential credentials, enterprise logs, and database backups.

Tagged:how to format json in terminal with jqclean messy minified json onlinefix invalid json syntax trailing commarfc 8259 json format validationjavascript number max safe integer bigint bugprivate json formatter in browser

Experience This In Your Browser (Zero Upload)

Launch the interactive in-browser utilities referenced in this article:

View All 38 Tools →

Further Reading & Dispatches