JSON for Configuration Files

Learn best practices for using JSON as a configuration file format. Understand its limitations, when to use alternatives, and how popular tools use JSON configs.

Concept

Detailed Explanation

JSON is widely used for configuration files in the JavaScript ecosystem and beyond. Files like package.json, tsconfig.json, .eslintrc.json, composer.json, and .prettierrc are ubiquitous in modern development. While JSON's simplicity makes it a natural choice, its limitations as a configuration format have led to ongoing debates about alternatives.

Why JSON is popular for configuration:

JSON's biggest advantage as a config format is universality. Every programming language has a JSON parser, making it easy to read configuration from any environment. The syntax is simple enough that non-programmers can edit it with minimal training. JSON files can be validated against JSON Schema, enabling IDE autocompletion and real-time validation. There is no ambiguity in parsing — unlike YAML, JSON never silently converts types.

Popular JSON config files:

  • package.json (Node.js) — Project metadata, dependencies, scripts
  • tsconfig.json (TypeScript) — Compiler options (actually JSONC, supports comments)
  • .eslintrc.json (ESLint) — Linting rules and overrides
  • .prettierrc (Prettier) — Code formatting options
  • manifest.json (Chrome extensions, PWAs) — Application metadata
  • appsettings.json (ASP.NET) — Application configuration

Limitations of JSON for configuration:

The lack of comments is the most significant limitation. Configuration files benefit enormously from inline documentation explaining why certain values were chosen, what the defaults are, and what alternatives were considered. Standard JSON provides no mechanism for this. Other limitations include the inability to reference other values within the file (no variables or interpolation), no support for multi-line strings, and the strict syntax requirements (no trailing commas) that make hand-editing error-prone.

Common mistakes developers make:

A major mistake is storing environment-specific values (database URLs, API keys, secrets) in JSON config files that get committed to version control. Use environment variables or secret management tools instead. Another mistake is creating deeply nested configuration structures that are hard to override or merge. Flat or shallowly nested configs are easier to work with. Developers also sometimes create configuration files that grow to hundreds or thousands of lines, making them difficult to maintain — consider splitting into multiple files by concern.

Best practices:

Use JSON Schema for your config files to enable IDE autocompletion and validation. Keep configurations shallow and well-documented (in a separate README if needed). Use environment variables for secrets and environment-specific values, with JSON for defaults. If comments are essential, consider JSONC (for tools that support it) or YAML as alternatives. Validate configuration at application startup and fail fast with clear error messages for missing or invalid values.

Use Case

Creating a package.json file for a new Node.js project with scripts, dependencies, and metadata, while keeping environment-specific settings in .env files.

Try It — JSON Formatter

Open full tool