What Makes JSON "Valid"?
JSON validity is defined by RFC 8259, the current internet standard. A JSON document is valid if and only if:
- It contains a single top-level value (object, array, string, number, boolean, or null).
- All strings are enclosed in double quotes, not single quotes.
- All object keys are strings, wrapped in double quotes.
- Commas separate items in arrays and objects, but never appear after the final item.
- Brackets
{}and[]are balanced and correctly nested. - Numbers follow the standard grammar — no leading zeros (except for 0), no hex, no NaN, no Infinity.
- Special characters in strings are escaped:
\\n,\\t,\\",\\\\.
Anything that violates these rules is rejected by the JSON.parse function in browsers, json.loads in Python, and every other conforming parser. The error typically includes a character position so you can jump straight to the problem.
Reading a JSON Parse Error
A typical browser error looks like:
Uncaught SyntaxError: Unexpected token } in JSON at position 87The important parts are the token (}— a closing brace where the parser expected something else) and the position (character offset from the start of the string). To find character 87 in your JSON, use your editor's Go to Character feature, or open DevTools console:
const raw = '...your JSON here...';
console.log(raw.substring(80, 100)); // show a 20-char window around position 87Node.js and Python give similar error formats. jq's error output includes the line and column, which is often more useful for multi-line JSON.
Top 10 JSON Syntax Errors (Ranked by Frequency)
1. Trailing comma
// INVALID
{ "items": [1, 2, 3,] }
// VALID
{ "items": [1, 2, 3] }By far the most common error. JavaScript objects allow trailing commas, JSON does not.
2. Single quotes
// INVALID
{ 'name': 'Alice' }
// VALID
{ "name": "Alice" }3. Unquoted keys
// INVALID
{ name: "Alice" }
// VALID
{ "name": "Alice" }4. Comments
// INVALID — JSON has no comments
{
"port": 8080, // server port
"debug": true
}
// VALID — remove the comment
{ "port": 8080, "debug": true }5. Unescaped double quote inside a string
// INVALID
{ "quote": "She said "hi"" }
// VALID
{ "quote": "She said \"hi\"" }6. Unescaped newline inside a string
// INVALID — literal newline inside string
{
"message": "hello
world"
}
// VALID — escape as \n
{ "message": "hello\nworld" }7. JavaScript values (undefined, NaN, Infinity)
// INVALID
{ "value": undefined, "ratio": NaN }
// VALID — use null or a string
{ "value": null, "ratio": null }8. Leading zero in a number
// INVALID
{ "code": 007 }
// VALID
{ "code": 7 }
// or as a string
{ "code": "007" }9. Trailing content after the JSON
Anything after the top-level closing bracket is invalid — no HTML footer, no semicolon, no extra data.
10. Empty input
An empty string is not valid JSON. The minimum valid JSON is a single value likenull, 0, "", {}, or [].
Validating JSON in Every Language
JavaScript / Node.js
function isValidJson(str) {
try {
JSON.parse(str);
return true;
} catch (e) {
console.error('Invalid JSON:', e.message);
return false;
}
}
isValidJson('{"a": 1}'); // true
isValidJson("{'a': 1}"); // false, single quotes not allowedPython
import json
def is_valid_json(s):
try:
json.loads(s)
return True
except json.JSONDecodeError as e:
print(f'Invalid: {e.msg} at line {e.lineno} col {e.colno}')
return False
is_valid_json('{"a": 1}') # True
is_valid_json('{"a": 1,}') # False, trailing commaGo
import "encoding/json"
func isValidJSON(s string) bool {
var v interface{}
return json.Unmarshal([]byte(s), &v) == nil
}PHP
function isValidJson(string $s): bool {
json_decode($s);
return json_last_error() === JSON_ERROR_NONE;
}Validation in CI/CD Pipelines
Add JSON validation to your continuous integration to catch broken config files before they hit production. A one-line check with jq or Python fails the build on any invalid JSON:
# GitHub Actions example
- name: Validate JSON configs
run: |
for f in $(find . -name "*.json" -not -path "./node_modules/*"); do
jq empty "$f" || { echo "INVALID: $f"; exit 1; }
doneKey Facts
- Standard:
- RFC 8259 (strict JSON, no comments, no trailing commas)
- Engine:
- Native JSON.parse — same parser used by browsers & Node.js
- Error info:
- Position of first syntax error, plus friendly explanation
- Privacy:
- Runs in your browser — nothing sent to any server
- Beyond syntax:
- For schema validation use Ajv (JS) or jsonschema (Python)
- Cost:
- Free, no signup, no ads on the tool
Related JSON Tools
- Format JSON Online — beautify JSON with proper indentation
- JSON Viewer Online — explore JSON structure interactively
- JSON Lint Online — deeper JSONLint-style error reporting
- Fix Invalid JSON — troubleshooting guide for malformed JSON
- Minify JSON Online — compress JSON for production
- Escape JSON String — safely embed strings in JSON