AspNetCore.Docs
AspNetCore.Docs copied to clipboard
Official stance on comments in appsettings.json
Given that comments work in appsettings.json and it's a pretty common question too, it would be useful to clarify the official stance on this page.
One very helpful possibility would be to instruct how to configure the JSON configuration provider to explicitly support comments.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 0eb4d762-3910-2c57-8741-3306c15537b6
- Version Independent ID: ba2254a0-ea8e-9b28-e94b-edd0fc58c043
- Content: Configuration in ASP.NET Core
- Content Source: aspnetcore/fundamentals/configuration/index.md
- Product: aspnet-core
- Technology: aspnetcore-fundamentals
- GitHub Login: @Rick-Anderson
- Microsoft Alias: riande
Hello @kimjamia ... This comes up every few years, and it is a good time to hear if there are any updates from the team. I recall back in 2015 👴😄 the following discussion: https://github.com/aspnet/Announcements/issues/24#issuecomment-101431570
Reminder on the history (from Wikipedia):
Comments were intentionally excluded from JSON. In 2012, Douglas Crockford described his design decision thus: "I removed comments from JSON because I saw people were using them to hold parsing directives, a practice which would have destroyed interoperability."
@JamesNK ... What's the latest news on comments in JSON files? Are the concerns still that they aren't spec and external parsers may choke (per Eilon's remark 7 years ago) ... so the team still generally frowns on the idea?
Comments are intentionally allowed in appsettings.json. You can add that to the docs.
What are the rules for use so that the parser doesn't choke?
- Must they be on their own line? ... or can they appear at the ends of lines, too?
- Is /* ... */ syntax also supported? ... or only //? Does the // need to be at any specific position (e.g., are leading spaces ok)?
- Should they avoid JSON delimiters (e.g., {, }, [, ], commas)? ... and/or any other chars?
JSON comments behave like JS (or C#). Both styles are supported.
https://www.w3schools.com/js/js_comments.asp
for vscode add below to settings.json
"files.associations": {
"appsettings*.json": "jsonc"
}
Comments are intentionally allowed in appsettings.json. You can add that to the docs.
BTW... to get Visual Studio Code to stop complaining, do this: https://stackoverflow.com/questions/47834825/in-vs-code-disable-error-comments-are-not-permitted-in-json/47834826#47834826
Another option is to rename your appsettings.json
to appsettings.jsonc
(and your environment specific config files as well, so appsettings.Development.json
becomes appsettings.Development.jsonc
, and so on).
Then add this near the top of Program.cs:
var builder = WebApplication.CreateBuilder(args);
IHostEnvironment env = builder.Environment;
builder.Configuration
.AddJsonFile("appsettings.jsonc", true, true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.jsonc", true, true);
Then right click on each appsettings file and make sure to set Copy to output directory to Copy if newer, as otherwise these files no longer automatically get copied if you change the extension.
Now you can add comments to all of your jsonc files, and not have to instruct other developers to change their editor settings to get the file to show up without syntax highlighting errors.