JSON injection attacks
November 4, 2026 · 15 min read
JSON is data, not executable logic - but applications stitch JSON into queries, HTML, and shell commands in dangerous ways. JSON injection covers attacker-controlled strings that break out of intended structure or ride into SQL and NoSQL queries embedded in JSON columns.
Threat model for JSON APIs
Attackers send extra keys, unexpected types, or crafted strings in JSON bodies. Without schema validation, servers may pass those values into dynamic SQL, LDAP filters, or template engines. Treat every JSON field as untrusted input even when Content-Type is application/json.
SQL injection through JSON fields
Storing JSON in relational columns then querying with string concatenation is risky: WHERE data->>'name' = ' + userInput allows quote escape. Use parameterized queries, ORM bindings, and jsonb operators with bound parameters. Never build SQL from raw JSON text blobs.
-- Unsafe pattern (conceptual)
-- "name": "x' OR '1'='1"
-- Safe: parameterized json path
SELECT * FROM orders WHERE payload->>'name' = $1;
Client-side JSON and DOM sinks
Assigning parsed JSON to innerHTML or eval-like sinks enables XSS when strings contain HTML. Use text nodes or framework escaping. JSONP and legacy callbacks are largely gone but document any custom script protocols.
Unsafe deserialization and type gadgets
Parsing JSON into arbitrary object graphs (Java polymorphic types, pickle in Python if misused) can trigger gadget chains. Use DTOs, disable default typing, and validate with JSON Schema at the boundary.
Sanitize, validate, and least privilege
- Validate syntax with a strict parser, then validate shape with JSON Schema.
- Reject unknown fields on public APIs (
additionalProperties: false). - Map to primitive types and enums in application code - do not pass raw JsonNode into queries.
Paste suspicious payloads into the JSON validator to confirm structure before reproducing in staging exploit tests.
FAQ
- Can JSON itself execute code?
- Standard JSON.parse does not execute code. Risk is in how apps use parsed values afterward.
- Does NoSQL prevent injection?
- No. MongoDB and others suffer injection when queries concatenate user strings into operators.
- Should I strip HTML from JSON strings?
- Escape on output context (HTML, URL, JS). Validate allowed character sets at input if business rules require.
- Are comments in JSON a security issue?
- Non-standard parsers that allow comments can disagree with strict validators - stick to RFC 8259 at boundaries.
Related: JSON Schema tutorial · Common JSON validation errors · How to debug JSON APIs · Invalid JSON causes