Introduction
JSON (JavaScript Object Notation) is everywhere. APIs, config files, databases, you name it. Yet many developers trip over the same basic pitfalls. Let's fix that with practical, no-nonsense advice.
Know Your Types
JSON supports only six types: string, number, boolean, null, object, and array. No dates, no undefined, no functions. If you need a date, use a string in ISO 8601 format (e.g., "2025-03-15T10:30:00Z"). If you need a binary blob, use base64 encoding.
{
"name": "Alice",
"age": 30,
"isActive": true,
"metadata": null,
"tags": ["dev", "json"],
"address": {
"city": "Berlin"
}
}
Validate Early, Validate Often
Never trust external JSON. Always validate before use. In JavaScript, use JSON.parse() inside a try-catch. In Python, use json.loads() with proper error handling.
// JavaScript
function safeParse(jsonString) {
try {
return JSON.parse(jsonString);
} catch (e) {
console.error('Invalid JSON:', e.message);
return null;
}
}
# Python
import json
def safe_parse(json_string):
try:
return json.loads(json_string)
except json.JSONDecodeError as e:
print(f"Invalid JSON: {e}")
return None
Use Schema Validation for Complex Data
For anything beyond a trivial structure, use a schema validator. JSON Schema is the standard. It catches missing fields, wrong types, and value constraints.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer", "minimum": 0 },
"email": { "type": "string", "format": "email" }
},
"required": ["name", "email"]
}
In Python, use jsonschema library. In JavaScript, use ajv.
Handle Missing Fields Gracefully
Don't assume all fields exist. Use optional chaining (?.) in JavaScript or dict.get() in Python.
// JavaScript
const city = data?.address?.city ?? 'Unknown';
# Python
city = data.get('address', {}).get('city', 'Unknown')
Pretty Print for Debugging
When logging JSON, format it for readability. Both JSON.stringify and json.dumps support indentation.
// JavaScript
console.log(JSON.stringify(data, null, 2));
# Python
print(json.dumps(data, indent=2))
Avoid Common Pitfalls
- Trailing commas: JSON does not allow them. Use a linter.
- Single quotes: JSON requires double quotes for strings and keys.
- Comments: JSON has no comments. Use a separate metadata field if needed.
- Nested depth: Some parsers limit depth (e.g., 512 levels). Keep it shallow.
Serialize Custom Objects
When converting custom objects to JSON, define a serialization method. In JavaScript, override toJSON(). In Python, use a custom encoder.
// JavaScript
class User {
constructor(name, age) {
this.name = name;
this.age = age;
}
toJSON() {
return { name: this.name, age: this.age };
}
}
# Python
import json
class UserEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, User):
return {"name": obj.name, "age": obj.age}
return super().default(obj)
Use Streaming for Large Files
For huge JSON files (gigabytes), don't load everything into memory. Use streaming parsers like ijson (Python) or stream-json (Node.js).
# Python with ijson
import ijson
with open('large.json', 'r') as f:
for item in ijson.items(f, 'item'):
process(item)
Conclusion
Working with JSON confidently means understanding its limitations, validating early, handling errors gracefully, and using the right tools for the job. These practices will save you hours of debugging and make your code more robust. Now go forth and parse with confidence!
Top comments (0)