Development2024-05-08

My Debug Notebook: 12 Common JSON Errors and How I Fixed Them

After getting roasted by my tech lead for the 5th JSON-related bug, I started keeping a notebook. Here are the 12 most common JSON errors I've encountered.

#json#debugging#errors#javascript#api

又被组长批了。"这个 JSON 解析错误你怎么又犯了?"

Fine. I started keeping a bug notebook. Every JSON error I encounter goes in. After 6 months, here are the 12 most common ones — with fixes.

Error 1: Trailing Commas

{ "name": "John", "age": 30, }  // ❌ Invalid
{ "name": "John", "age": 30 }   // ✅ Valid

JSON does NOT allow trailing commas. JavaScript does. That's why it works in Node.js but fails in JSON.parse().

Error 2: Single Quotes

{ 'name': 'John' }  // ❌ Invalid
{ "name": "John" }  // ✅ Valid

JSON requires double quotes. Always.

Error 3: Unquoted Keys

{ name: "John" }    // ❌ Invalid (works in JS object literal)
{ "name": "John" }  // ✅ Valid JSON

Error 4: Comments

{ "name": "John" /* this is a comment */ }  // ❌ Invalid
{ "name": "John" }  // ✅ No comments allowed in JSON

JSON does NOT support comments. Use JSONC (JSON with Comments) if you need them.

Error 5: NaN and Infinity

{ "value": NaN }        // ❌ Invalid
{ "value": Infinity }   // ❌ Invalid
{ "value": null }       // ✅ Use null instead

Error 6: Date Objects

{ "date": new Date() }           // ❌ Not valid JSON
{ "date": "2024-02-18T00:00:00Z" } // ✅ ISO 8601 string

Error 7: Undefined Values

{ "value": undefined }  // ❌ Not valid JSON
{ "value": null }       // ✅ Use null
// Or just omit the key entirely

Error 8: Circular References

const obj = { name: "John" };
obj.self = obj; // Circular!
JSON.stringify(obj); // TypeError: Converting circular structure to JSON

Fix: Use a replacer function or a library like flatted.

Error 9: Encoding Issues

{ "text": "Hello â€" World" }  // ❌ Mojibake
{ "text": "Hello — World" }    // ✅ Proper UTF-8

Always ensure UTF-8 encoding throughout your pipeline.

Error 10: Large Numbers

{ "id": 9007199254740993 }  // ❌ Precision loss in JavaScript
{ "id": "9007199254740993" } // ✅ Use string for large numbers

JavaScript's Number.MAX_SAFE_INTEGER is 2^53 - 1. Beyond that, precision is lost.

Error 11: BOM Character

{ "name": "John" }  // ❌ BOM at start causes parse error
{ "name": "John" }   // ✅ No BOM

UTF-8 BOM (Byte Order Mark) is invisible but breaks JSON parsers.

Error 12: Mixed Content Types

{ "items": [1, "two", null, {"four": 4}] }  // ✅ Valid but fragile

Arrays with mixed types are valid JSON but cause runtime errors when you assume all elements are the same type.

My Debugging Workflow

  1. Paste the JSON into a JSON Formatter — it highlights the exact error location
  2. Check for the 12 errors above
  3. Validate against a JSON Schema to catch structural issues
  4. Compare with expected output using JSON Compare

Validate and fix your JSON instantly with our free JSON Formatter — highlights errors, beautifies output, and validates structure.

🛠

Try It Yourself

Put what you've learned into practice with our free online tools.

Explore More Developer Tools

Discover more tools and tutorials in this category

Browse Category →