JSON Beginners Guide - Syntax, Data Types, and Common Errors

Published 2025-02-18 · ToolNest

JSON (JavaScript Object Notation) is the most common data format for APIs and web applications. This guide covers the essentials.

What is JSON?

JSON is a lightweight, text-based format for storing and transporting data. It's human-readable and machine-parseable, making it the de facto standard for API responses.

JSON Syntax Rules

  1. Data is in key-value pairs: "name": "ToolNest"
  2. Keys must be in double quotes (single quotes not allowed)
  3. Values can be strings, numbers, booleans, null, arrays, or objects
  4. Items are separated by commas
  5. Objects use {} curly braces, arrays use [] square brackets
  6. No trailing commas allowed
  7. No comments allowed in strict JSON

JSON Data Types

Type Example Description
String "hello" Text, must use double quotes
Number 42 or 3.14 Integer or float, no quotes
Boolean true / false No quotes
Null null Represents no value
Array [1, 2, 3] Ordered list
Object {"key": "value"} Key-value collection

Example JSON

{
  "name": "ToolNest",
  "version": 1,
  "tools": ["word-counter", "json-formatter"],
  "active": true,
  "description": null
}

JSON vs JavaScript Object

JSON looks like a JavaScript object, but there are differences:

Feature JSON JavaScript Object
Keys Must be double-quoted Can be unquoted
Comments Not allowed Allowed
Trailing comma Not allowed Allowed
Functions Not allowed Allowed as values
Undefined Not allowed Allowed

Common JSON Errors

  1. Single quotes: 'key': 'value' → Use double quotes instead
  2. Trailing comma: {"a": 1,} → Remove the last comma
  3. Unquoted keys: {name: "John"} → Use {"name": "John"}
  4. Comments: // comment → JSON doesn't support comments
  5. Special characters: Use Unicode escapes for control characters

Working with JSON in JavaScript

// Parse JSON string to object
const obj = JSON.parse('{"name":"ToolNest"}');

// Convert object to JSON string
const str = JSON.stringify(obj, null, 2);  // 2 = pretty-print with 2 spaces

// Validate JSON
try {
  JSON.parse(input);
  console.log("Valid JSON");
} catch (e) {
  console.log("Invalid: " + e.message);
}

Use ToolNest JSON Formatter to beautify, minify, and validate your JSON data.

← Back to Articles