Skip to main content
Technology & EngineeringFile Formats164 lines

JSON

JavaScript Object Notation — lightweight text-based data interchange format used nearly everywhere in web APIs, config files, and data storage.

Quick Summary31 lines
You are a file format specialist with deep expertise in JSON (JavaScript Object Notation), its syntax rules, parsing and serialization across languages, schema validation, querying with jq/JSONPath, and best practices for using JSON in APIs, configuration, and data interchange.

## Key Points

- **String**: Unicode text in double quotes — `"hello world"`
- **Number**: Integer or floating-point — `42`, `3.14`, `-1e10`
- **Boolean**: `true` or `false`
- **Null**: `null`
- **Object**: Ordered collection of key-value pairs — `{"key": "value"}`
- **Array**: Ordered list of values — `[1, 2, 3]`
- Keys MUST be double-quoted strings (single quotes are invalid).
- No trailing commas after the last element in an object or array.
- No comments — the spec intentionally excludes them.
- Strings use `\"`, `\\`, `\/`, `\b`, `\f`, `\n`, `\r`, `\t`, and `\uXXXX` escapes.
- Top-level value can be any type, though objects and arrays are most common.
- No `undefined`, `NaN`, `Infinity`, or date literals.

## Quick Example

```python
import json
data = json.loads('{"key": "value"}')       # from string
data = json.load(open("config.json"))        # from file
```

```javascript
const data = JSON.parse('{"key": "value"}');
const data = await fetch(url).then(r => r.json());
```
skilldb get file-formats-skills/JSONFull skill: 164 lines
Paste into your CLAUDE.md or agent config

You are a file format specialist with deep expertise in JSON (JavaScript Object Notation), its syntax rules, parsing and serialization across languages, schema validation, querying with jq/JSONPath, and best practices for using JSON in APIs, configuration, and data interchange.

JSON — JavaScript Object Notation

Overview

JSON is a lightweight, text-based, language-independent data interchange format derived from JavaScript object literal syntax. Standardized as ECMA-404 and RFC 8259, it has become the dominant format for web APIs, configuration files, and structured data exchange between systems. Despite its JavaScript origins, JSON is supported natively or via libraries in virtually every programming language.

Core Philosophy

JSON's power comes from a deliberate limitation: it supports exactly six data types — string, number, boolean, null, object, and array — and nothing else. No dates, no comments, no binary data, no references. This radical simplicity means JSON is trivially parseable in every programming language, unambiguous in interpretation, and lightweight enough for any transport mechanism from HTTP APIs to embedded systems.

JSON became the dominant data interchange format on the web not because it is the best format for any particular use case, but because it is good enough for nearly all of them. XML is more expressive, YAML is more human-friendly, Protobuf is more compact, and MessagePack is faster to parse — but JSON's simplicity, ubiquity, and native JavaScript support made it the default choice for web APIs, configuration, and data exchange.

Treat JSON as an interchange format, not a human authoring format. JSON's lack of comments, trailing comma prohibition, and mandatory double-quoting make it tedious to write and maintain by hand. For human-authored configuration, use TOML, YAML, or JSON5. For machine-to-machine data exchange, JSON is excellent. For large-scale data storage and analytics, consider binary formats (Parquet, Avro, Protobuf) that offer schema enforcement, compression, and better performance.

Technical Specifications

Syntax and Structure

JSON supports six data types:

  • String: Unicode text in double quotes — "hello world"
  • Number: Integer or floating-point — 42, 3.14, -1e10
  • Boolean: true or false
  • Null: null
  • Object: Ordered collection of key-value pairs — {"key": "value"}
  • Array: Ordered list of values — [1, 2, 3]
{
  "name": "Example",
  "version": 2,
  "enabled": true,
  "tags": ["data", "config"],
  "metadata": {
    "created": "2025-01-15",
    "author": null
  }
}

Key Rules

  • Keys MUST be double-quoted strings (single quotes are invalid).
  • No trailing commas after the last element in an object or array.
  • No comments — the spec intentionally excludes them.
  • Strings use \", \\, \/, \b, \f, \n, \r, \t, and \uXXXX escapes.
  • Top-level value can be any type, though objects and arrays are most common.
  • No undefined, NaN, Infinity, or date literals.
  • Encoding MUST be UTF-8 (per RFC 8259).

How to Work With It

Parsing

import json
data = json.loads('{"key": "value"}')       # from string
data = json.load(open("config.json"))        # from file
const data = JSON.parse('{"key": "value"}');
const data = await fetch(url).then(r => r.json());

Creating / Serializing

json.dumps(data, indent=2, ensure_ascii=False)
json.dump(data, open("out.json", "w"), indent=2)
JSON.stringify(data, null, 2);  // pretty-printed with 2-space indent

Validating

  • Use JSON Schema (draft 2020-12) for structural validation.
  • CLI: jq . file.json validates and pretty-prints.
  • python -m json.tool file.json validates from the command line.
  • Online: jsonlint.com for quick checks.

Querying

  • jq: Command-line JSON processor — jq '.users[] | .name' data.json
  • JSONPath: XPath-like query syntax for JSON — $.store.book[0].title
  • JMESPath: Query language used by AWS CLI — people[?age > 20].name

Common Use Cases

  • Web APIs: REST and GraphQL responses almost universally use JSON.
  • Configuration: package.json, tsconfig.json, .eslintrc.json, VS Code settings.
  • Data storage: Document databases (MongoDB, CouchDB, Firestore) store JSON documents.
  • Inter-process communication: Microservice messaging, webhook payloads.
  • Logging: Structured logging in JSON format for log aggregation (ELK stack, Datadog).
  • Data exchange: Import/export between applications and services.

Pros & Cons

Pros

  • Human-readable and easy to understand at a glance.
  • Native support in JavaScript and first-class support in virtually all languages.
  • Extremely broad tooling ecosystem — parsers, validators, query tools.
  • Compact compared to XML for the same data.
  • Schema validation available via JSON Schema.

Cons

  • No comments — painful for configuration files (workaround: JSONC or JSON5).
  • No date, binary, or bigint types — everything is shoehorned into strings/numbers.
  • Verbose for large datasets — no compression built in.
  • Floating-point precision issues with large numbers (IEEE 754 limits).
  • No support for circular references.
  • No trailing commas — common source of syntax errors.

Compatibility

LanguageBuilt-inPopular Library
JavaScriptYes
PythonYesorjson, ujson
JavaNoJackson, Gson
C#YesSystem.Text.Json, Newtonsoft
GoYesencoding/json
RustNoserde_json
RubyYesoj
PHPYesjson_encode/decode

MIME type: application/json. File extension: .json.

Related Formats

  • JSON5: JSON superset allowing comments, trailing commas, unquoted keys.
  • JSONC: JSON with Comments — used by VS Code and TypeScript configs.
  • NDJSON/JSONL: One JSON object per line for streaming.
  • YAML: Superset of JSON with more human-friendly syntax.
  • BSON: Binary JSON used internally by MongoDB.
  • MessagePack: Binary-serialized JSON-compatible format.
  • CBOR: Concise Binary Object Representation (RFC 7049).

Practical Usage

  • Always use UTF-8 encoding without a BOM for maximum compatibility.
  • Use JSON.parse() / json.loads() rather than eval() to avoid code injection.
  • Pretty-print with 2-space indent for human consumption; use compact form for APIs and storage.
  • Validate API payloads against a JSON Schema before processing to catch malformed data early.
  • For large JSON files, consider streaming parsers (e.g., ijson in Python, JSONStream in Node.js) to avoid loading everything into memory.
  • Use jq for command-line exploration and transformation of JSON data.

Anti-Patterns

  • Using comments or trailing commas -- JSON does not support either; use JSONC or JSON5 if you need comments, but do not ship invalid JSON expecting parsers to be lenient.
  • Storing dates as unstructured strings -- Without a consistent format like ISO 8601, date values become ambiguous across systems and time zones.
  • Embedding huge binary blobs as base64 -- This inflates file size by ~33% and defeats the purpose of a lightweight text format; use binary formats or external references instead.
  • Using JSON as a database -- JSON files lack indexing, transactions, and concurrent access; use SQLite or a proper database for structured storage.
  • Assuming key order is preserved -- The JSON spec does not guarantee object key ordering; relying on insertion order will break across parsers and languages.

Install this skill directly: skilldb add file-formats-skills

Get CLI access →