What is JSON? Complete Guide with Examples

๐Ÿ“– 9 min read ยท Data Formats ยท Try JSON Formatter โ†’

What is JSON?

JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that is easy for humans to read and write, and easy for machines to parse and generate. It was created by Douglas Crockford in the early 2000s and has become the dominant format for data exchange on the web, replacing XML in most modern applications.

Despite its name, JSON is language-independent. It is supported natively or through libraries in virtually every programming language including Python, Java, PHP, Ruby, Go, Rust, C#, and of course JavaScript.

JSON Data Types

JSON supports exactly six data types:

String"Hello, World!"

Text enclosed in double quotes. Supports Unicode. Escape special characters with backslash.

Number42, 3.14, -7, 1.5e10

Integer or floating-point. No distinction between int and float. No NaN or Infinity.

Booleantrue, false

Lowercase only. true or false. No quotes.

Nullnull

Represents an empty or absent value. Lowercase only.

Object{"key": "value"}

Unordered collection of key-value pairs. Keys must be strings in double quotes.

Array[1, "two", true, null]

Ordered list of values. Can contain any mix of data types including nested objects and arrays.

JSON Syntax Rules

JSON has strict syntax rules. Violating any of these causes a parse error:

  • Keys must be strings wrapped in double quotes (single quotes are not valid)
  • Strings must use double quotes โ€” single quotes are not allowed
  • No trailing commas after the last item in an object or array
  • No comments โ€” JSON does not support // or /* */ comments
  • Numbers cannot have leading zeros (e.g., 007 is invalid)
  • Special characters in strings must be escaped with backslash: \", \\, \/, \b, \f, \n, \r, \t

JSON Object Example

{
  "user": {
    "id": 1,
    "name": "Jane Doe",
    "email": "jane@example.com",
    "age": 28,
    "isActive": true,
    "address": {
      "street": "123 Main St",
      "city": "New York",
      "country": "US"
    },
    "tags": ["developer", "designer"],
    "lastLogin": null
  }
}

JSON vs XML

JSON has largely replaced XML for web APIs because it is more concise and easier to work with. Here is the same data in both formats:

JSON (47 characters)

{
  "name": "Jane",
  "age": 28
}

XML (60 characters)

<user>
  <name>Jane</name>
  <age>28</age>
</user>

JSON is ~30% more compact, natively supported in JavaScript, and easier to parse in most languages. XML still has advantages for document-centric data, namespaces, and schemas.

Common JSON Errors and How to Fix Them

Unexpected token

Cause: Usually a missing comma, extra comma, or unquoted key

Fix: Check for trailing commas and ensure all keys are in double quotes

Unexpected end of JSON input

Cause: Missing closing bracket, brace, or quote

Fix: Count your opening and closing brackets/braces to ensure they match

Invalid escape character

Cause: Unescaped backslash or invalid escape sequence in a string

Fix: Escape backslashes as \\ and ensure escape sequences are valid

Duplicate keys

Cause: Two keys with the same name in an object

Fix: JSON technically allows duplicate keys but behavior is undefined โ€” remove duplicates

JSON Best Practices

  • Use camelCase for key names in JavaScript APIs (e.g., firstName, not first_name)
  • Use snake_case for Python and Ruby APIs following their conventions
  • Always validate JSON before parsing in production โ€” use try/catch around JSON.parse()
  • Use ISO 8601 format for dates: "2024-01-15T10:30:00Z"
  • Keep JSON responses flat when possible โ€” deeply nested structures are harder to work with
  • Use pagination for large arrays instead of returning thousands of items at once
  • Minify JSON in production to reduce payload size, but keep it formatted in development

Try Our JSON Tools

Format, validate, and convert JSON instantly in your browser.