JSON Validation Best Practices
JavaScript Object Notation (JSON) is the universal language of the web. However, many developers conflate native JavaScript object syntax with strict JSON, leading to massive API failures. When debugging with a strict JSON Formatter, certain syntax errors appear constantly.
1. The Trailing Comma Trap
In modern JavaScript, adding a trailing comma to the last property of an object or array is perfectly legal and even recommended for cleaner Git diffs. In JSON, it is a fatal parsing error.
{
"name": "John",
"age": 30, // FATAL: Trailing comma
}
2. Double Quotes Are Mandatory
JSON requires all string values and all keys to be wrapped in strict double quotes ("). Single quotes (') or backticks (`) are invalid.
// INVALID
{ 'name': 'John' }
// VALID
{ "name": "John" }
3. Handling Escaped Characters
If your JSON string contains double quotes, tabs, or newlines, you must properly escape them. A raw newline inside a string value will crash JSON.parse().
// INVALID
{ "bio": "Hello
World" }
// VALID
{ "bio": "Hello\nWorld" }
4. Valid Data Types
JSON only supports a subset of data types: Strings, Numbers, Objects, Arrays, Booleans, and null. It does not support undefined, Functions, Dates (must be stringified as ISO 8601), or NaN.