jsondebuggingsyntax errorsbest practicesbeginners

20 Common JSON Mistakes and How to Avoid Them

·8 min read

The Most Common JSON Mistakes

JSON looks simple but has sharp edges. Here are the 20 mistakes developers hit most often, with fixes for each.

Syntax Mistakes

Mistake 1: Trailing comma

The single most common JSON error. JavaScript allows trailing commas in arrays and objects; JSON does not.

json
// WRONG
{ "name": "Ravi", "age": 28, }

// RIGHT
{ "name": "Ravi", "age": 28 }

Mistake 2: Single quotes instead of double quotes

JSON requires double quotes for all strings and all keys. Single quotes are valid JavaScript but invalid JSON.

json
// WRONG
{ 'name': 'Ravi' }

// RIGHT
{ "name": "Ravi" }

Mistake 3: Unquoted keys

JSON keys must always be quoted strings. Unquoted keys are valid JavaScript object literals but not JSON.

json
// WRONG
{ name: "Ravi", age: 28 }

// RIGHT
{ "name": "Ravi", "age": 28 }

Mistake 4: Comments

JSON has no comment syntax. Adding // or /* */ causes a parse error in every JSON parser.

json
// WRONG
{
  // user identifier
  "id": "usr_123"
}

// RIGHT — use a _comment key as a workaround
{
  "_comment": "user identifier",
  "id": "usr_123"
}

Mistake 5: Undefined value

undefined is a JavaScript concept — it does not exist in JSON. Using it causes silent data loss via JSON.stringify.

javascript
// WRONG — middleName disappears from output
const user = { name: "Ravi", middleName: undefined };
JSON.stringify(user); // '{"name":"Ravi"}'

// RIGHT — use null to represent absence of a value
const user = { name: "Ravi", middleName: null };
JSON.stringify(user); // '{"name":"Ravi","middleName":null}'

Mistake 6: NaN and Infinity

JavaScript's special number values NaN, Infinity, and -Infinity are not valid JSON values.

javascript
JSON.stringify({ score: NaN });      // '{"score":null}'
JSON.stringify({ value: Infinity }); // '{"value":null}'
// Both silently become null — use a sentinel number instead

Mistake 7: Using a function as a value

Functions cannot be serialized to JSON. They are silently dropped by JSON.stringify.

javascript
const obj = { name: "Ravi", greet: () => "Hello" };
JSON.stringify(obj); // '{"name":"Ravi"}' — greet is dropped

Structure Mistakes

Mistake 8: Duplicate keys

JSON parsers technically allow duplicate keys but behaviour is undefined — most use the last value, some throw, and some use the first. Never rely on duplicate keys.

json
// WRONG — undefined which "name" wins
{ "name": "Ravi", "name": "Mehta" }

// RIGHT
{ "firstName": "Ravi", "lastName": "Mehta" }

Mistake 9: Mixing arrays and objects

A JSON array contains ordered values; a JSON object contains named key-value pairs. Mixing them up causes parse errors.

json
// WRONG — array cannot have key-value pairs
["name": "Ravi", "age": 28]

// RIGHT — object for named fields
{ "name": "Ravi", "age": 28 }

Mistake 10: Missing comma between elements

Forgetting commas between object properties or array elements is a very common mistake when editing JSON by hand.

json
// WRONG
{ "name": "Ravi" "age": 28 }

// RIGHT
{ "name": "Ravi", "age": 28 }

Encoding Mistakes

Mistake 11: Unescaped special characters in strings

Double quotes, backslashes, and control characters inside strings must be escaped.

json
// WRONG — unescaped quote breaks the string
{ "message": "He said "hello" to me" }

// RIGHT
{ "message": "He said "hello" to me" }

Mistake 12: Unescaped backslash in Windows paths

json
// WRONG — backslash is the escape character in JSON strings
{ "path": "C:Users
aviile.json" }

// RIGHT
{ "path": "C:\Users\ravi\file.json" }

Mistake 13: Non-UTF-8 encoding

JSON must be encoded in UTF-8 (or UTF-16/32 with a BOM, though UTF-8 is universal). Files saved in Latin-1 or Windows-1252 will fail when they contain non-ASCII characters like accented letters.

Data Type Mistakes

Mistake 14: Numbers as strings

Storing numbers as quoted strings means consumers must parse them. Use the native JSON number type.

json
// WRONG
{ "age": "28", "price": "99.99" }

// RIGHT
{ "age": 28, "price": 99.99 }

Mistake 15: Booleans as strings

json
// WRONG
{ "active": "true", "verified": "false" }

// RIGHT
{ "active": true, "verified": false }

Mistake 16: Null as the string "null"

json
// WRONG — this is the four-character string "null", not a null value
{ "middleName": "null" }

// RIGHT
{ "middleName": null }

API Design Mistakes

Mistake 17: Inconsistent date formats

Pick one format and use it everywhere. ISO 8601 (2025-01-15T14:32:07Z) is the standard.

json
// WRONG — mix of formats across fields
{ "createdAt": "Jan 15 2025", "updatedAt": 1705330327 }

// RIGHT
{ "createdAt": "2025-01-15T14:32:07Z", "updatedAt": "2025-01-15T16:00:00Z" }

Mistake 18: Returning an array at the top level without metadata

Plain arrays make it impossible to add pagination or metadata later without a breaking change.

json
// WRONG — breaking change to add pagination later
[{ "id": 1 }, { "id": 2 }]

// RIGHT — envelope allows non-breaking additions
{ "data": [{ "id": 1 }, { "id": 2 }], "total": 2 }

Mistake 19: Using 0/1 instead of booleans

json
// WRONG — not idiomatic and requires consumers to cast
{ "isActive": 1, "isVerified": 0 }

// RIGHT
{ "isActive": true, "isVerified": false }

Mistake 20: Deeply nested JSON without a reason

Nesting adds complexity. Flatten where possible, and only nest when it represents a real parent-child relationship.

json
// AWKWARD — three levels deep for a simple value
{ "user": { "address": { "city": { "name": "Ahmedabad" } } } }

// CLEANER
{ "city": "Ahmedabad" }

Quick Fix Checklist

  • Run your JSON through a validator to catch syntax errors instantly
  • Use JSON.stringify(obj, null, 2) to pretty-print and spot structural issues
  • Replace undefined values with null before serializing
  • Use ISO 8601 for all date and time fields
  • Wrap all API arrays in a data envelope

Paste your JSON into JSONKit's validator for instant error detection with exact line numbers.

Try JSON Validator

Paste your JSON to find syntax errors, trailing commas, and more — instantly.