← All posts
Developer·

JSON for Beginners: Syntax, Use Cases, and Common Mistakes

JSONprogrammingweb developmentbeginners

What is JSON?

JSON (JavaScript Object Notation) is a lightweight text format for storing and transporting data. Despite its name, JSON is language-independent and used across virtually every programming language and platform.

If you've ever used a web API, configured a VS Code setting, or looked at a package.json file — you've already worked with JSON.

JSON Syntax

JSON has only two structures:

Objects — unordered collections of key-value pairs:

{
  "name": "Alice",
  "age": 30,
  "active": true
}

Arrays — ordered lists of values:

["apple", "banana", "cherry"]

Data Types

JSON supports exactly six data types:

  1. String: "hello" (must use double quotes)
  2. Number: 42, 3.14, -1 (no hex, no NaN, no Infinity)
  3. Boolean: true or false
  4. Null: null
  5. Object: {"key": "value"}
  6. Array: [1, 2, 3]

That's it. No dates, no functions, no undefined, no comments.

Where JSON is Used

  • REST APIs: The standard format for request and response bodies.
  • Configuration files: package.json, tsconfig.json, .eslintrc.json.
  • Data storage: MongoDB stores documents in a JSON-like format (BSON).
  • Logging: Structured logging uses JSON for machine-parseable log entries.
  • Data interchange: Exporting/importing data between systems.

Common Mistakes

1. Using single quotes Wrong: {'name': 'Alice'} Right: {"name": "Alice"}

JSON requires double quotes for both keys and string values. This is the most common error when hand-writing JSON.

2. Trailing commas Wrong: {"a": 1, "b": 2,} Right: {"a": 1, "b": 2}

JavaScript allows trailing commas. JSON does not.

3. Unquoted keys Wrong: {name: "Alice"} Right: {"name": "Alice"}

In JavaScript objects, unquoted keys are fine. In JSON, every key must be a double-quoted string.

4. Comments JSON does not support comments of any kind. If you need comments in a config file, consider JSONC (JSON with Comments) which tools like VS Code support, or use YAML instead.

JSON vs XML

JSON largely replaced XML for data interchange because it's more compact and easier to read:

Feature JSON XML
Readability High Moderate
Verbosity Low High
Data types Built-in All text
Arrays Native Requires markup
Parsing Fast Slower

Debugging JSON Errors

When you get a parse error, the error message usually includes a position number. Count characters from the start to find the issue. Common culprits:

  • A missing comma between items
  • A trailing comma after the last item
  • A missing closing brace or bracket
  • A single-quoted string
  • An unescaped special character in a string

Use our JSON Formatter to validate and format your JSON instantly.