FormatForge

What is JSON?

JSON (JavaScript Object Notation) is a lightweight, text-based data format used for storing and exchanging data. It's the most popular format for APIs and configuration files due to its simplicity and universal support across programming languages.

JSON Syntax

JSON syntax is derived from JavaScript object notation, but it's stricter. Here are the key rules:

  • Data is in key/value pairs
  • Keys must be strings in double quotes
  • Data is separated by commas
  • Curly braces hold objects
  • Square brackets hold arrays

Basic JSON Example

{
  "name": "John Doe",
  "age": 30,
  "email": "john@example.com",
  "isActive": true,
  "address": {
    "city": "New York",
    "country": "USA"
  },
  "hobbies": ["reading", "gaming", "hiking"]
}

JSON Data Types

JSON supports six data types:

TypeDescriptionExample
StringText in double quotes"Hello World"
NumberInteger or floating-point42, 3.14, -17
Booleantrue or falsetrue, false
NullEmpty valuenull
ObjectKey-value pairs in curly braces{"key": "value"}
ArrayOrdered list in square brackets[1, 2, 3]

Common JSON Use Cases

1. REST APIs

JSON is the standard format for REST API responses. When you fetch data from a web service, it typically returns JSON:

// API Response
{
  "status": "success",
  "data": {
    "users": [
      {"id": 1, "name": "Alice"},
      {"id": 2, "name": "Bob"}
    ]
  }
}

2. Configuration Files

Many applications use JSON for configuration, like package.json in Node.js or tsconfig.json in TypeScript:

{
  "name": "my-project",
  "version": "1.0.0",
  "dependencies": {
    "react": "^18.2.0",
    "next": "^14.0.0"
  }
}

3. Data Storage

NoSQL databases like MongoDB store data in JSON-like documents, and localStorage in browsers uses JSON for storing complex data.

JSON Best Practices

  • Always use double quotes for strings and keys (single quotes are invalid)
  • No trailing commas after the last item in objects or arrays
  • No comments - JSON doesn't support comments (use JSON5 or YAML if you need them)
  • Use consistent indentation for readability (2 or 4 spaces)
  • Validate your JSON before using it in production

Common JSON Errors

{'name': 'John'}

Single quotes are invalid - use double quotes

{"items": [1, 2, 3,]}

Trailing commas are not allowed

{name: "John"}

Keys must be quoted strings

JSON vs Other Formats

FeatureJSONXMLYAML
ReadabilityGoodModerateExcellent
File SizeCompactVerboseCompact
CommentsNoYesYes
Schema SupportJSON SchemaXSD, DTDLimited

Learn more about these formats:

Frequently Asked Questions

What does JSON stand for?

JSON stands for JavaScript Object Notation. It's a lightweight data format that's easy for humans to read and write, and easy for machines to parse and generate.

Is JSON the same as JavaScript?

No, JSON is not JavaScript. JSON is a data format that was derived from JavaScript syntax, but it's language-independent and used across virtually all programming languages.

What is JSON used for?

JSON is primarily used for transmitting data between a server and web application (APIs), storing configuration files, and as a data exchange format between different systems.

Is JSON better than XML?

JSON is often preferred over XML for web APIs because it's more compact and easier to parse in JavaScript. However, XML is better for document markup and has features like namespaces and schemas that JSON lacks.

JSON Tools