Writing Comments in JSON Causes Read Errors: How to Separate Configuration and Description

プログラミング・Web開発カテゴリを表すパンダのイラスト Programming / Web Development

About This ArticleThis article is created using an automated generation workflow leveraging generative AI. The procedures are explained using hypothetical examples.

Verification Status: 📘 Primary Source Referenced, Unverified in Production Environment

I added a short note, "extend wait time only on weekends," to a configuration file being handed over to a successor. The next day, the program failed to read that configuration. Even if the configuration values remain unchanged, loading stops if the description format deviates from the rules. Standard JSON does not have a syntax for writing comments.

Keep the Original File and Compare Only a Single Comment Line

Prepare the following JSON in a practice copy.timeout_seconds is a hypothetical configuration for wait time, and notifications is a hypothetical configuration for notification status.

{"timeout_seconds": 30, "notifications": true}

As-is, it can be parsed by standard JSON parsing routines, such as JavaScript's JSON.parse. Next, edit only the copy by adding true after } and before the closing brace // 通知を有効化. While this is written in a way that would be read as a comment in JavaScript code, a standard JSON parser cannot load this copy.

{
  "timeout_seconds": 30,
  "notifications": true // 通知を有効化
}

To compare the two, pass each as a string to JSON.parse for practice. The first is successfully parsed as an object, but the second results in a syntax error. The error message varies depending on the processing engine. There is no need to replace your production configurations.

Changes made to the copyParsable in standard JSON
Formatted for readability using line breaks and whitespaceReadable
Followed by a value with // and added an explanation using Unreadable
Added a comma to the last item as wellUnreadable
Enclosed keys in single quotes instead of double quotesUnreadable

If a line number is provided in the error, check that line and the preceding one. For example, even if the closing bracket } is pointed to, check whether there is an extra comma right before it. Fixing multiple places at once makes it impossible to know what the cause was. Once you remove the comments and it becomes readable, perform the task of changing the wait time separately.

flowchart LR
  A["元の設定を保管"] --> B["コピーに変更を一つ加える"]
  B --> C["読込みを確認"]
  C --> D["前後の行と元の設定を照合"]

Place explanations in the procedure manual alongside the configuration

Whether explanation keys can be added to the configuration file is determined by the application being used. It does not necessarily ignore unknown keys. If the specification cannot be verified, avoid forcing it into JSON and instead write it in the README or operational notes in the same folder.

For example, "Target: report.json of timeout_seconds. Normally 30 seconds. If extending for the weekend, record the reason and the previous value. Return to the original value on Monday morning." Writing the target file and item together prevents confusion when similar settings exist in other folders. If settings are under version control, reviewing the configuration and explanation as the same change makes it easier to trace which version the explanation applied to later.

flowchart TD
  A["設定に理由を残したい"] --> B{"アプリが説明用キーを認めているか"}
  B -->|確認済み| C["仕様に従って記録"]
  B -->|不明・使えない| D["隣の手順書にファイル名と項目を書く"]

Depending on the editor, documents containing comments can be opened as JSONC. However, the absence of warnings in the editing screen is different from whether the application actually using it can read it. If passing the same file to a different application, verify the format expected by the recipient.

In the explanation, write not only the date of the operation but also when the setting is to be reverted. For example, writing "Changed to extend Friday processing. Revert to the previous value on Monday morning" allows the successor to determine whether that value should be kept the next week as well. To avoid using the same value in a different environment with a similar setting name, distinguish whether the target is for testing or production.

Even if the syntax is valid, the configuration is not necessarily correct

The wait time of 30 to "30" changes both to be readable as JSON. However, the former is a number and the latter is a string. If the application expects a number, the string format may not work.

If someone reports that notifications do not arrive after fixing the configuration, first determine whether the error occurred during the loading phase or if notifications were disabled after loading. For the former, check the syntax such as comments and commas; for the latter, check the notification control items and configuration values. Changing both at once makes it impossible to trace why the file became readable or why notifications were restored.

Verification consists of two stages: first, whether it can be read as standard JSON; second, whether the item names, value types, and units comply with the application specifications. Separating fixes to make the file readable from modifications to the values minimizes the rollback scope if an issue occurs. This example illustrates syntax differences and is not a record of verified behavior for any specific application.

flowchart LR
  A["文法を確認"] --> B["項目と値の種類を確認"]
  B --> C["アプリの練習環境で確認"]

The successor checks the file name specified in the notes to verify whether the same values remain in the configuration. Instead of directly restoring values found only in the notes, they compare the versions before and after the modification before restoring them.

References

Glossary

  • JSON: A format for recording values in a fixed syntax and exchanging them between programs.

  • Key: A name assigned to a value. In this example, timeout_seconds。

  • Syntax: The formatting rules that allow a program to read a document.

  • JSONC: A JSON-based format that allows comments. It is not necessarily treated the same as standard JSON.

Document information

Article title
Writing Comments in JSON Causes Read Errors: How to Separate Configuration and Description
Published
Updated
Source
https://papanda925.com/?p=19218&lang=en

License: Text and original figures for which this site holds the relevant rights are available under CC BY 4.0 , unless otherwise noted. This article may include content created or edited with generative AI. If code has a separate license notice or a linked GitHub repository license, that license takes precedence for the code. Quotations, third-party materials, images, and trademarks are excluded from this license. Usage policy

Copied title and URL