Search Authority

How to Comment in JSON: Quick Guide & Best Practices

JSON represents data as lightweight text, making it ideal for configuration, APIs, and web messaging. Understanding how to add comments in JSON helps teams annotate intent and c...

Mara Ellison
How to Comment in JSON: Quick Guide & Best Practices

JSON represents data as lightweight text, making it ideal for configuration, APIs, and web messaging. Understanding how to add comments in JSON helps teams annotate intent and clarify structure without breaking parsing rules.

Because JSON formally excludes native comment syntax, developers rely on documented workarounds and tool support. This guide explains practical approaches and common patterns for annotating JSON files in real projects.

Purpose Method Tool Support Risk
Human notes Use separate documentation High None
Inline explanation Add a _comment key Medium Low if ignored by parser
Temporary drafts Use JSON5 or Hjson Variable Medium if tooling mismatched
Strict validators Schema-driven descriptions High None

Understanding JSON Comment Limitations

The JSON specification does not allow comments inside objects or arrays. Standard parsers will throw an error if they encounter # or // characters that are not inside string values.

Teams often choose external annotation strategies to keep files machine-safe while still providing guidance for engineers. Recognizing these limits early prevents fragile workarounds in production pipelines.

Using a _comment Key Pattern

Adding a "_comment" property is a widespread convention to embed notes directly in JSON objects. Because keys with that name are still valid data, many linters allow it while reserving it for human-readable hints.

Place "_comment" at the top of an object or near related settings so readers quickly see the intent. Ensure your validation rules ignore this key if you enforce strict schemas.

Leveraging External Documentation

Keeping a README or separate spec file next to the JSON provides a clean, tool-agnostic way to explain sections. Markdown tables, bullet lists, and diagrams can clarify complex defaults without risking parser errors.

Link external docs from within JSON using a "_documentation" field pointing to a wiki page or internal URL. This keeps machine data minimal while giving engineers a clear path to context.

Exploring JSON5 and Alternative Formats

JSON5 extends JSON to support line comments, trailing commas, and unquoted keys. Useful in development scripts, JSON5 requires parsers that explicitly support the relaxed syntax, so it is not suitable for public APIs.

Alternatives like Hjson and YAML offer human-friendly comment syntax and are ideal for config files moved into strict pipelines via transforms. Evaluate compatibility before adopting these formats in shared services.

Schema-Driven Annotation Strategies

JSON Schema allows descriptions at the property level, enabling tools to generate forms, docs, and validation hints without inline comments. This approach keeps behavior explicit and machine-readable while serving as structured annotation.

Use "title" and "description" fields in your schema to explain format, units, and expected ranges. Pair examples in the schema to show practical usage alongside formal constraints.

Key Approaches to Annotating JSON

  • Prefer external documentation for rich explanations and diagrams
  • Use _comment or _documentation keys sparingly with clear conventions
  • Adopt JSON Schema descriptions for machine-readable annotations
  • Reserve JSON5 or Hjson for local development and CI transforms
  • Align tooling and linter rules to prevent accidental comment syntax in production JSON
  • Validate that parsers and generators accept your chosen annotation style
  • Keep examples close to schemas so engineers see both intent and valid values

FAQ

Reader questions

Can I use // or /* */ inside a JSON file like in JavaScript?

No, standard JSON parsers will fail if they encounter // or /* */ because those characters are outside the grammar. Use a separate text file or a format like JSON5 if you need classic line or block comments.

Is adding a _comment key safe for all systems?

It is safe only when every tool in your pipeline tolerates extra keys. Some strict schemas or generated code may reject unrecognized properties, so validate this behavior before relying on _comment.

How can I document my JSON without modifying the file? . Maintain a companion Markdown or YAML specification and use a naming convention like .json.meta alongside the data file. Automation can merge human notes at build time while keeping the runtime JSON clean. Will JSON5 solve my commenting needs in production?

JSON5 introduces line comments and other conveniences, but production APIs and strict contracts usually require canonical JSON. Reserve JSON5 for development stages and transform it to standard JSON before deployment.

Related Reading

More pages in this topic cluster.

Who Designed the Nike Logo? The Story Behind the Swoosh

The Nike swoosh is one of the most recognizable symbols in the world, but few people know the story behind its creation. This piece explores who designed the Nike logo, why it h...

Read next
What is the World's Hottest Pepper? 🌶️🔥

When people ask about the world's hottest pepper, they usually mean the variety that currently holds the Guinness World Record and pushes the boundaries of capsaicin heat. Peppe...

Read next
Jon Huertas in This Is Us:角色, 出演时期与剧情影响详解

Jon Huertas 在《这就是我们》中饰演成年 Kevin Pearson,这一角色从2016年首播持续至2022年最终季,构成了剧集核心家庭叙事的重要组成部�...

Read next