Dev.to WebDev 🛠 Dev 👁 0 📖 1 min read

JSON Validator & Formatter: How to Fix Invalid JSON Fast (Without Installing Anything)

JSON is everywhere — APIs, config files, package.json — and it breaks in the most annoying ways. Here are the 5 errors I see most often, and the fastest ways to find and fix them without installing anything. 1.

JSON is everywhere — APIs, config files, package.json — and it breaks in the most annoying ways. Here are the 5 errors I see most often, and the fastest ways to find and fix them without installing anything.

1. Trailing commas

{
  "name": "Sujal",
  "city": "Jaipur",
}

That comma after "Jaipur" kills the whole file. JSON does not allow trailing commas, period. Delete it.

2. Single quotes instead of double quotes

{ 'name': 'Sujal' }

Looks fine, is not JSON. Keys AND string values must use double quotes:

{ "name": "Sujal" }

3. Unquoted keys

{ name: "Sujal" }

Valid JavaScript object, invalid JSON. Quote every key.

4. Comments

{
  // user details
  "name": "Sujal"
}

JSON has no comments. If you need them, use JSONC in your editor — but strip them before sending to an API.

5. NaN, Infinity, undefined

{ "score": NaN }

These are JavaScript things, not JSON things. Use null or a number.

The fastest ways to validate

Browser console (zero setup): paste your JSON into JSON.parse('...'). The error message tells you the exact character position where it broke. That's usually enough.

Python (already on most machines):

echo '{"a":1,}' | python3 -m json.tool

It pretty-prints valid JSON and points at the error for invalid JSON.

jq (if you live in the terminal):

echo '{"a":1}' | jq .

Formatting: pick one style and stick to it

2-space indent is the most common. Tabs are fine too. What matters is consistency — mixed indentation makes diffs unreadable and code reviews painful.

A real before/after

Before:

{
  'users': [
    { name: 'Amit', age: 24, },
    { name: 'Priya', age: 29, },
  ],
}

After:

{
  "users": [
    { "name": "Amit", "age": 24 },
    { "name": "Priya", "age": 29 }
  ]
}

Four fixes: quotes, keys, trailing commas, and it's done.

I got tired of switching between five different sites for this kind of work, so I built BulkTextKit — 15 free text tools in the browser, including a JSON formatter/validator that points at the exact error. Pro (batch processing, presets, CSV export) is $9 until Oct 19: https://bulktextkit.gt.tc

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.