Skip to content
Security

JSON Security Best Practices for Developers

·8 min read
JSON Security Best Practices for Developers - Security guide from JSON Prettifier

Quick answer: JSON itself is safe; the risk comes from how it is built, parsed, merged, and exposed. Always build JSON with a serializer (never string concatenation), parse with JSON.parse() (never eval()), block __proto__ and constructor keys when merging untrusted objects, and return explicit field lists from APIs instead of raw database records. On the server, enforce Content-Type: application/json, cap body size and nesting depth, validate parsed data against a schema, and verify JWT signatures with a whitelist of algorithms.

In 2021, a popular npm package with 7 million weekly downloads was found vulnerable to prototype pollution. The fix was one line. The damage was months of incident response.

JSON is not dangerous. How we handle JSON is dangerous. This guide covers the vulnerabilities that actually appear in production - not theoretical edge cases, but the mistakes that have caused real breaches.

For browser-facing APIs, pair these controls with the JSON API CORS mistakes guide. CORS determines which origins may read a response in browser JavaScript, but it does not replace authentication, authorization, or CSRF protection.

After removing secrets and personal data, inspect the sample in the JSON Viewer and use JSON Compare to review the redacted result.

JSON Injection

JSON injection happens when untrusted input is concatenated directly into a JSON string without proper escaping. This is the JSON equivalent of SQL injection:

// VULNERABLE: String concatenation to build JSON
const userInput = 'Alice", "role": "admin", "extra": "';
const json = '{"name": "' + userInput + '", "role": "viewer"}';

// Result:
// {"name": "Alice", "role": "admin", "extra": "", "role": "viewer"}
// Most parsers use the LAST occurrence of a key, so role = "viewer"
// But some parsers use the FIRST - role = "admin"

That parser disagreement is a security problem in its own right: a payload with two role keys can pass validation in one layer and be read differently in another. The behavior differs by language and library, as detailed in how parsers handle duplicate keys in JSON.

The fix is simple: never construct JSON by string concatenation. Always use JSON.stringify():

// SAFE: Let the serializer handle escaping
const data = {
  name: userInput,
  role: "viewer"
};
const json = JSON.stringify(data);
// Special characters in userInput are properly escaped

Prototype Pollution

For executable boundary tests that distinguish parsed keys, altered target prototypes, and inherited reads, use the five JSON prototype pollution checks before merging API data.

Prototype pollution is a JavaScript-specific attack where an attacker injects properties into Object.prototype through JSON input. Any code that recursively merges objects without safeguards is potentially vulnerable:

// A naive deep merge function
function merge(target, source) {
  for (const key in source) {
    if (typeof source[key] === "object" && source[key] !== null) {
      target[key] = target[key] || {};
      merge(target[key], source[key]);
    } else {
      target[key] = source[key];
    }
  }
  return target;
}

// Attacker sends this JSON:
const malicious = JSON.parse('{"__proto__": {"isAdmin": true}}');
merge({}, malicious);

// Now EVERY object in the application has isAdmin = true
const user = {};
console.log(user.isAdmin); // true

Defenses include:

  • Check for __proto__, constructor, and prototype keys and reject or skip them during object merging.
  • Use Object.create(null) for objects that store untrusted data - these have no prototype chain to pollute.
  • Freeze the prototype: Object.freeze(Object.prototype) prevents any modifications, though this can break libraries that rely on prototype extension.
  • Use Map instead of plain objects for user-controlled key-value data.

The same caution applies to copying objects. A hand-rolled recursive copy has the same blind spot as the merge above; the options compared in how to deep clone JSON in JavaScript (including structuredClone) show which ones are safe to use with untrusted input.

Unsafe Deserialization

Some JSON-like parsers in other languages can instantiate arbitrary objects during deserialization. This is extremely dangerous because it can lead to remote code execution.

Python: Never use yaml.load() (which can execute arbitrary Python) when you mean yaml.safe_load(). For JSON specifically, Python's json module is safe - it only produces basic types.

Java: Libraries like Jackson can be configured to instantiate arbitrary classes based on type hints in the JSON. Always disable polymorphic deserialization or restrict it to a whitelist of allowed types:

// DANGEROUS: Unrestricted polymorphic deserialization
objectMapper.enableDefaultTyping();

// SAFE: Restrict to specific base types
objectMapper.activateDefaultTyping(
  objectMapper.getPolymorphicTypeValidator(),
  ObjectMapper.DefaultTyping.NON_FINAL,
  JsonTypeInfo.As.PROPERTY
);

Never Use eval() for JSON

This one should be well-known by now, but it still appears in production code:

// NEVER DO THIS
const data = eval("(" + jsonString + ")");

// jsonString could contain:
// (function() { /* malicious code */ })()

// ALWAYS use JSON.parse()
const data = JSON.parse(jsonString);

JSON.parse() only understands the JSON grammar. It cannot execute functions, access variables, or perform any computation. It either returns a valid JavaScript value or throws a SyntaxError. There is no scenario where eval() is the right choice for parsing JSON.

Preventing Data Leaks in API Responses

One of the most common JSON security mistakes isn't a parsing vulnerability - it's returning too much data. Serializing a full database object directly to JSON exposes internal fields that clients shouldn't see:

// DANGEROUS: Serializing the full user object
app.get("/api/users/:id", async (req, res) => {
  const user = await db.users.findById(req.params.id);
  res.json(user);
  // Exposes: passwordHash, internalNotes, ssn, stripeCustomerId, etc.
});

// SAFE: Explicitly select fields to return
app.get("/api/users/:id", async (req, res) => {
  const user = await db.users.findById(req.params.id);
  res.json({
    id: user.id,
    name: user.name,
    email: user.email,
    role: user.role,
    createdAt: user.createdAt
  });
});

Use a serialization layer or DTO (Data Transfer Object) pattern consistently. Never rely on database field exclusion alone - a schema migration that adds a sensitive column will automatically leak it if you're serializing whole objects. The same discipline applies to logs, bug reports, and support tickets: before JSON leaves your system in any form, run it through the patterns in how to redact sensitive data from JSON.

Content-Type Validation

Your server should validate the Content-Type header on incoming requests and reject anything unexpected. Accepting JSON from a request with a text/plain content type can bypass CORS preflight checks:

// Express middleware to enforce JSON Content-Type
function requireJson(req, res, next) {
  if (req.method !== "GET" && req.method !== "DELETE") {
    const contentType = req.headers["content-type"];
    if (!contentType || !contentType.includes("application/json")) {
      return res.status(415).json({
        error: "Unsupported Media Type. Expected application/json"
      });
    }
  }
  next();
}

CORS preflight requests (the OPTIONS check) are only triggered for certain content types. application/json triggers a preflight; text/plain does not. An attacker can exploit this difference to send cross-origin requests with JSON payloads by using a "simple" content type. Strict Content-Type validation on the server blocks this vector. For the full picture of what the header should look like, including charset and +json vendor suffixes, see the JSON Content-Type guide.

Rate Limiting and Resource Exhaustion

JSON APIs that accept complex input are vulnerable to resource exhaustion attacks. An attacker can send deeply nested JSON, extremely large payloads, or high volumes of requests to overwhelm your server:

// Set a request body size limit
app.use(express.json({ limit: "1mb" }));

// Validate nesting depth
function checkDepth(obj, maxDepth = 10, current = 0) {
  if (current > maxDepth) {
    throw new Error("JSON nesting depth exceeds maximum");
  }
  if (typeof obj === "object" && obj !== null) {
    for (const value of Object.values(obj)) {
      checkDepth(value, maxDepth, current + 1);
    }
  }
}

A particularly nasty attack is sending a JSON payload like {"a":{"a":{"a":{"a":...}}}} nested hundreds of levels deep. Some parsers handle this fine; others will blow the stack or consume excessive memory. Always enforce a body size limit and consider limiting nesting depth for untrusted input. Choosing a sensible limit depends on what your parser can realistically handle, which JSON maximum file size walks through for browsers, Node.js, and common frameworks.

Input Validation After Parsing

Parsing JSON successfully doesn't mean the data is valid. Always validate the structure and content after parsing:

import { z } from "zod";

const CreateUserSchema = z.object({
  name: z.string().min(1).max(100),
  email: z.string().email(),
  age: z.number().int().min(0).max(150).optional(),
  role: z.enum(["viewer", "editor", "admin"]).default("viewer")
});

app.post("/api/users", (req, res) => {
  const result = CreateUserSchema.safeParse(req.body);
  if (!result.success) {
    return res.status(422).json({
      error: "Validation failed",
      details: result.error.issues
    });
  }
  // result.data is typed and validated
  createUser(result.data);
});

JWT Security Considerations

JSON Web Tokens are base64-encoded JSON payloads with a signature. (If you want to see exactly what is inside one, decoding a JWT takes a few lines of code - which is precisely why the points below matter.) A few critical security points:

  • Never trust the payload without verifying the signature. The JWT body is base64-encoded, not encrypted. Anyone can decode and read it. Anyone can modify it. The signature is the only thing that proves authenticity.
  • Reject the "none" algorithm. Some JWT libraries accept {"alg": "none"} in the header, which skips signature verification entirely. Always whitelist acceptable algorithms.
  • Don't store sensitive data in JWT payloads. JWTs are not encrypted by default (JWE exists but is less common). Don't put passwords, API keys, or PII in the payload.
  • Set short expiration times. JWTs can't be revoked once issued (without a server-side blocklist), so short expiration times limit the damage window.
// Verifying a JWT securely
import jwt from "jsonwebtoken";

try {
  const payload = jwt.verify(token, publicKey, {
    algorithms: ["RS256"],  // Whitelist allowed algorithms
    issuer: "auth.example.com",
    audience: "api.example.com"
  });
} catch (err) {
  // Token is invalid, expired, or tampered with
  return res.status(401).json({ error: "Invalid token" });
}

Security Checklist

A quick reference for securing JSON in your applications:

  • Use JSON.parse(), never eval()
  • Build JSON with serializers, never string concatenation
  • Block __proto__ and constructor in object merging
  • Return explicit field lists in API responses, not raw database objects
  • Validate Content-Type: application/json on incoming requests
  • Enforce body size limits and nesting depth limits
  • Validate all parsed JSON against a schema before processing
  • Wrap JSON responses in objects, not bare arrays
  • Verify JWT signatures and whitelist algorithms
  • Rate-limit all public-facing JSON endpoints

JSON itself is a simple, safe format. The vulnerabilities come from how it's constructed, parsed, merged, and exposed. For structural validation, consider JSON Schema to enforce data contracts. And for a quick reference on syntax issues that cause parsing failures, see our guide to common JSON errors.

For signed payloads, follow these seven JSON canonicalization checks to distinguish unchanged webhook bodies from canonical signing inputs.

For deployment settings, use these seven JSON config file checks to keep secrets out of committed files and reject unsafe effective configuration before startup.

For event receivers, review these six JSON webhook mistakes before changing raw-body middleware or acknowledging unpersisted work.

For field-direction boundaries, run these five JSON Schema readOnly and writeOnly tests to verify request rejection, public response construction, nested fields, and endpoint enforcement.

Frequently Asked Questions

What is JSON injection?
JSON injection happens when untrusted input is concatenated directly into a JSON string, letting an attacker close a value and add their own keys, such as a role of admin. The fix is to never build JSON by string concatenation and always use a serializer like JSON.stringify(), which escapes special characters correctly.
What is prototype pollution and how do I prevent it?
Prototype pollution is a JavaScript attack where JSON containing a __proto__ key is deep-merged into an object, adding properties to Object.prototype and therefore to every object in the application. Prevent it by rejecting __proto__, constructor, and prototype keys during merges, storing untrusted data in Object.create(null) objects or Maps, or freezing Object.prototype.
Is it safe to use eval() to parse JSON?
No. eval() executes arbitrary JavaScript, so a malicious payload can run code. JSON.parse() only understands the JSON grammar and either returns a value or throws a SyntaxError; there is no scenario where eval() is the right choice for parsing JSON.
Why should an API validate the Content-Type header on incoming JSON?
Browsers only send a CORS preflight for certain content types. application/json triggers one, but text/plain does not, so an attacker can send a cross-origin JSON payload with a simple content type and skip the preflight. Rejecting requests that are not application/json with a 415 closes that gap.
Can I trust the data inside a JWT?
Only after verifying the signature. A JWT payload is base64-encoded, not encrypted, so anyone can read or modify it. Verify with a whitelist of allowed algorithms (never accept alg none), check issuer and audience, keep expiration times short, and never put passwords, API keys, or PII in the payload.

100% Client-Side - Your Data Stays Private

Because our tool runs entirely in your browser, sensitive JSON payloads never touch a server. Paste confidential data with confidence.

Open JSON Prettifier