JSON is ubiquitous in web APIs, microservices, and client-server state synchronization. Mastering its quirks and optimization techniques will make you a far more effective software developer.
Key Techniques for Working with JSON#
1. Using JSON.stringify Space Parameter#
Did you know JSON.stringify takes a 3rd parameter for indentation?
const data = { name: "Ilustrado", active: true };
// Formats with 2 spaces
const prettyJson = JSON.stringify(data, null, 2);
console.log(prettyJson);
2. Custom Replacer Functions#
You can pass a replacer function as the 2nd argument to filter keys or sanitize sensitive fields like passwords:
const user = { name: "Sarah", passwordHash: "secret123", role: "admin" };
const sanitized = JSON.stringify(user, (key, value) => {
if (key === "passwordHash") return undefined; // Strips field
return value;
});
For large JSON objects, browser-native JSON processing is up to 10x faster than heavy third-party parsing libraries.
Conclusion#
Mastering native JSON features allows you to inspect and manipulate data effortlessly without adding bloat to your projects.