Back to Blog
Data Formats

Understanding JSON: A Complete Developer Guide

Learn the fundamentals of JSON, its syntax rules, common pitfalls, and how it became the backbone of modern web APIs.

By Birhrt Team

What is JSON and Why Does It Matter?

JSON (JavaScript Object Notation) is a lightweight data interchange format that has become the de facto standard for data communication on the web. Created by Douglas Crockford in the early 2000s, JSON was designed to be a minimal, readable, and portable text format for structuring data. Today, virtually every web API, configuration system, and NoSQL database uses JSON as its primary data format.

Before JSON, XML dominated the data interchange landscape. While XML is powerful, its verbose syntax — with opening and closing tags, attributes, and namespace declarations — made it heavy for network transmission and tedious for developers to write and read. JSON solved this by borrowing a subset of JavaScript's object literal syntax, creating a format that is both human-readable and machine-parseable.

The Six JSON Data Types

JSON supports exactly six data types, which is part of its elegance:

1. Strings — Enclosed in double quotes only. Single quotes are not valid JSON:

{ "name": "Alice" }

2. Numbers — Integers and floating-point values without quotes:

{ "age": 30, "temperature": 98.6 }

3. Booleanstrue or false, lowercase only:

{ "active": true, "deleted": false }

4. Null — Represents an intentional absence of value:

{ "middleName": null }

5. Arrays — Ordered lists enclosed in square brackets:

{ "colors": ["red", "green", "blue"] }

6. Objects — Unordered collections of key-value pairs:

{ "address": { "city": "Mumbai", "zip": "400001" } }

Notice what is missing: there is no date type, no undefined, no function, no comment syntax, and no support for single quotes, trailing commas, or unquoted keys. These constraints are intentional — they keep JSON simple and unambiguous.

Common JSON Pitfalls That Trip Up Developers

Even experienced developers regularly encounter these JSON parsing errors:

Trailing commas are the most frequent cause of JSON parse errors. JavaScript allows them, but JSON does not:

// ❌ Invalid JSON
{ "a": 1, "b": 2, }

// ✅ Valid JSON
{ "a": 1, "b": 2 }

Single quotes are another common mistake. JSON requires double quotes for all strings and keys:

// ❌ Invalid JSON
{ 'name': 'Alice' }

// ✅ Valid JSON
{ "name": "Alice" }

Comments are not supported in JSON. If you need comments in configuration files, consider JSONC (JSON with Comments) used by VS Code settings, or switch to YAML:

// ❌ Invalid JSON
{
  // This is a comment
  "key": "value"
}

Unquoted keys work in JavaScript but break JSON:

// ❌ Invalid JSON
{ name: "Alice" }

// ✅ Valid JSON
{ "name": "Alice" }

JSON in the Real World

REST APIs return JSON as the default response format. When you call fetch('https://api.example.com/users/1'), the response body is almost always JSON. The Content-Type: application/json header tells the client to parse the body as JSON.

Configuration files like package.json, tsconfig.json, .eslintrc.json, and Terraform state files use JSON because it is machine-readable and version-control friendly.

NoSQL databases like MongoDB, CouchDB, and Firebase Firestore store documents in JSON-like formats (BSON for MongoDB, which extends JSON with additional types like ObjectId, Date, and Binary).

Message queues like RabbitMQ and Apache Kafka frequently use JSON for message payloads because producers and consumers can be written in any programming language.

JSON vs. YAML vs. XML

FeatureJSONYAMLXML
ReadabilityGoodExcellentVerbose
CommentsNoYesYes
Data types610+Text only
File sizeSmallSmallestLargest
Parsing speedFastModerateSlow
Human editingModerateEasyHard

Choose JSON for API responses, data transmission, and when you need fast parsing. Choose YAML for configuration files where comments and readability matter (Kubernetes, Docker Compose, GitHub Actions). Choose XML when you need namespaces, schemas, or are working with legacy systems.

Working with JSON in JavaScript

JavaScript provides two built-in methods for JSON:

// Parse: String → Object
const obj = JSON.parse('{"name":"Alice","age":30}');

// Stringify: Object → String
const str = JSON.stringify(obj, null, 2); // 2-space indent

The second argument to JSON.stringify is a replacer function or array that controls which properties are included. The third argument controls indentation — set it to 2 for readable output or omit it for compact output.

Error handling is critical when parsing JSON from external sources:

try {
  const data = JSON.parse(userInput);
} catch (error) {
  // SyntaxError: tells you exactly where the parse failed
  console.error('Invalid JSON:', error.message);
}

JSON Schema: Validating Data Structure

JSON Schema is a vocabulary for annotating and validating JSON documents. It defines the expected structure, types, required fields, and constraints for a JSON object:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "name": { "type": "string", "minLength": 1 },
    "age": { "type": "integer", "minimum": 0 },
    "email": { "type": "string", "format": "email" }
  },
  "required": ["name", "email"]
}

JSON Schema is widely used in API documentation (OpenAPI/Swagger), form validation, and configuration file validation. Tools like AJV (Another JSON Validator) can validate data against schemas at runtime.

Performance Considerations

JSON parsing is one of the fastest serialization operations in JavaScript because JSON.parse() is implemented in native C++ code within the JavaScript engine. However, there are performance considerations:

  • Large payloads: Parsing 100MB+ JSON strings can block the main thread. Consider streaming parsers or Web Workers for large datasets.
  • Deep nesting: Extremely deep objects (100+ levels) can cause stack overflow in recursive parsers.
  • Repeated parsing: If you parse the same JSON multiple times, cache the parsed result rather than re-parsing.

Summary

JSON's simplicity is its superpower. With just six data types and a handful of syntax rules, it became the universal language of data exchange. Understanding its constraints — no trailing commas, double quotes only, no comments — helps you avoid common bugs and write cleaner data structures. Whether you are building REST APIs, writing configuration files, or storing data in NoSQL databases, JSON will remain a fundamental tool in your development workflow.

Try the Related Tool

Put this knowledge into practice with our free, privacy-first tool.

Open Json Formatter Tool →