JSON Syntax & Data Types

JSON basics with production pitfalls: numbers, strings, null, and how clients actually break.

On this page

JSON in APIs

JSON is the default payload format for web APIs because it's simple, widely supported, and human-readable. But “simple” doesn't mean “no pitfalls”. You must be strict about encoding and types.

Valid JSON types

  • object: { "key": "value" }
  • array: [1, 2, 3]
  • string: "text"
  • number: 123 or 12.34
  • boolean: true / false
  • null: null

Syntax rules that bite

  • Keys and strings must use double quotes (")
  • No trailing commas
  • Comments are not allowed
  • NaN/Infinity are not valid JSON numbers

Example: a typical API response

{
  "id": 42,
  "name": "Ada Lovelace",
  "tags": ["math", "history"],
  "active": true,
  "profile": null
}

Numbers: beware of precision

Many clients parse JSON numbers into IEEE-754 floating point. Large integers (like 64-bit IDs) can lose precision in JavaScript. A common solution is to send big IDs as strings.

Example: safe ID strategy

{
  "id": "9223372036854775807",
  "createdAt": "2026-02-14T12:00:00Z"
}

Strings and encoding

Use UTF-8. Avoid control characters. Always set Content-Type: application/json; charset=utf-8 (or at least application/json) and ensure your server actually outputs UTF-8.

Common mistakes

  • Returning “JSON-like” text with single quotes
  • Returning invalid numbers (NaN/Infinity)
  • Mixing types for the same field (sometimes number, sometimes string)
  • HTML error pages accidentally returned as “JSON”

Checklist

  • Payloads are strict JSON (no trailing commas, no comments)
  • IDs are consistent (string vs number decision is deliberate)
  • UTF-8 output is guaranteed