Best Practices ยท 8 min read
Following JSON best practices prevents bugs, improves maintainability, and ensures your data is production-ready. These guidelines are used by teams at companies building APIs consumed by millions of users.
Formatting Standards
- Use 2-space indentation consistently across your project
- Beautify during development; minify for production
- Validate JSON before every commit and deployment
- Keep one top-level object or array per file
- Use trailing newlines at end of JSON files
Naming Conventions
- camelCase: Standard for JavaScript APIs โ
firstName,isActive - snake_case: Common in Python/Ruby APIs โ
first_name,is_active - kebab-case: Rare in JSON keys but used in some configs
- Pick one convention and stick to it across your entire API
- Use descriptive key names โ
userEmailnotue
Errors to Avoid
- Trailing commas after last items in objects/arrays
- Mixing data types for the same key across records
- Using strings for numbers (or vice versa) inconsistently
- Deeply nested structures (keep under 5-6 levels when possible)
- Storing large binary data as base64 strings in JSON
- Including sensitive data (passwords, tokens) in JSON logs
Security Considerations
- Always validate JSON from untrusted sources before processing
- Limit accepted JSON payload size to prevent memory exhaustion
- Be cautious with deeply nested JSON (potential stack overflow)
- Sanitize string values before rendering in HTML (XSS prevention)
- Use HTTPS for all JSON API transmissions
- Never log full JSON payloads containing PII in production
Performance Tips
- Minify JSON for API responses in production
- Enable gzip/brotli compression at the server level
- Remove unnecessary fields before transmission
- Use pagination for large JSON arrays instead of sending everything
- Consider shorter key names only in high-throughput internal APIs
Development Workflow
- Write or receive JSON data
- Validate syntax with our JSON viewer
- Beautify for readability during review
- Test with JSON Schema if structural validation is needed
- Minify before deploying to production