Failed to load configuration from file '...\appsettings.json' means the file
exists but its JSON could not be parsed. The useful part is two lines further down, in
the inner JsonReaderException: it names the character and gives a
LineNumber and BytePositionInLine. Both count from 0, so add one
to find the line in your editor. In this test the cause was a missing comma between two
properties.
Add the comma and the app starts. A trailing comma or a comment, the suspects this test began with, was not the problem: both were accepted by the same app.
The error
dotnet run --no-build --no-launch-profile
Unhandled exception. System.IO.InvalidDataException: Failed to load configuration from file 'C:\...\fixes11\cfg\Cfg1\appsettings.json'.
---> System.FormatException: Could not parse the JSON file.
---> System.Text.Json.JsonReaderException: '"' is invalid after a value. Expected either ',', '}', or ']'. LineNumber: 4 | BytePositionInLine: 2.
at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
...
at Microsoft.AspNetCore.Builder.WebApplication.CreateBuilder(String[] args)
at Program.<Main>$(String[] args) in C:\...\fixes11\cfg\Cfg1\Program.cs:line 1
exit=-532462766
The paths are shortened; nothing else changed. The exception is thrown from
WebApplication.CreateBuilder, before any of your own code runs.
Why it happens
This was the file. The closing brace of Jwt on the fourth line has no comma
after it:
{
"Jwt": {
"Issuer": "fix11-test-issuer"
}
"AllowedHosts": "*"
}
The reader finished the value Jwt and then met " where it
expected a comma or a closing bracket, at LineNumber: 4 | BytePositionInLine: 2,
which is the start of "AllowedHosts" on the fifth line counted from one.
Seven versions of the file were run through the same app to see what this reader accepts:
- a trailing comma after the last property of an object: accepted
- a trailing comma after the last property of the root: accepted
- a
//line comment: accepted - a
/* */block comment: accepted - a missing comma between two properties: refused
- a single-quoted string: refused
- the valid file: accepted
The single-quoted value gave the same outer exception with
''' is an invalid start of a value. LineNumber: 2 | BytePositionInLine: 14.
inside.
The fix
{
"Jwt": {
"Issuer": "fix11-test-issuer"
},
"AllowedHosts": "*"
}
dotnet run --no-build --no-launch-profile
Jwt:Issuer = fix11-test-issuer
exit=0
One comma after the closing brace. Note where the error pointed: at the line after the one that needed the comma, because the missing comma was reported where the next property began.
How it was reproduced
A project from dotnet new web -f net10.0 with SDK 11.0.100 on Windows 11, no
packages added, the template's appsettings.Development.json moved out so only
one file was read. A small script wrote each version of appsettings.json,
printed it and ran the app once with --no-launch-profile; the program printed
Jwt:Issuer and exited.
Every command shown without a PS> marker ran in cmd: a PowerShell script passed it as cmd /c ... 2>&1 and printed the exit= line after it.
Frequently asked
- Can appsettings.json have comments?
- In this test on ASP.NET Core with net10.0, both // line comments and /* block comments */ in appsettings.json were accepted and the app started normally.
- Does appsettings.json allow a trailing comma?
- Yes in this test: a trailing comma after the last property of a nested object and after the last property of the root object were both accepted. A missing comma between two properties was refused.
- How do I find the error in Failed to load configuration from file?
- Read the inner JsonReaderException. It names the character it did not expect and gives LineNumber and BytePositionInLine, both counted from zero, so the problem is on that line plus one, or at the end of the line before it.
More decoded errors in the Fixes category. Trailing commas behave differently when your own code deserializes JSON: see the System.Text.Json trailing-comma error. If the file is not found at all, see The configuration file 'appsettings.json' was not found.