'.NET Core 3.1 CreateHostBuilder Cannot parse JSON file
I am experiencing an error when trying to run my ASP.Net Core 3.1 project. The error is at CreateHostBuilder
within Program.cs
public class Program {
public static void Main(string[] args) {
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
}
which has not been edited from the default ASP.NET Core Web application template from Visual Studio 2019. It gives the exception
System.FormatException: 'Could not parse the JSON file.'
It does not however refer exactly to what JSON file it's failing to parse. Here is a pastebin with all 3 existing JSON files it could be parsing.
Here is the full error stack:
HResult=0x80131537
Message=Could not parse the JSON file.
Source=Microsoft.Extensions.Configuration.Json
StackTrace:
at Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(Stream stream)
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Extensions.Configuration.FileConfigurationProvider.HandleException(ExceptionDispatchInfo info)
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load(Boolean reload)
at Microsoft.Extensions.Configuration.FileConfigurationProvider.Load()
at Microsoft.Extensions.Configuration.ConfigurationRoot..ctor(IList`1 providers)
at Microsoft.Extensions.Configuration.ConfigurationBuilder.Build()
at Microsoft.Extensions.Hosting.HostBuilder.BuildAppConfiguration()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at <filename>.Main(String[] args) in <filename>:line 13
This exception was originally thrown at this call stack:
System.Text.Json.ThrowHelper.ThrowJsonReaderException(ref System.Text.Json.Utf8JsonReader, System.Text.Json.ExceptionResource, byte, System.ReadOnlySpan<byte>)
System.Text.Json.Utf8JsonReader.ReadSingleSegment()
System.Text.Json.Utf8JsonReader.Read()
System.Text.Json.JsonDocument.Parse(System.ReadOnlySpan<byte>, System.Text.Json.Utf8JsonReader, ref System.Text.Json.JsonDocument.MetadataDb, ref System.Text.Json.JsonDocument.StackRowStack)
System.Text.Json.JsonDocument.Parse(System.ReadOnlyMemory<byte>, System.Text.Json.JsonReaderOptions, byte[])
System.Text.Json.JsonDocument.Parse(System.ReadOnlyMemory<char>, System.Text.Json.JsonDocumentOptions)
System.Text.Json.JsonDocument.Parse(string, System.Text.Json.JsonDocumentOptions)
Microsoft.Extensions.Configuration.Json.JsonConfigurationFileParser.ParseStream(System.IO.Stream)
Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider.Load(System.IO.Stream)
Inner Exception 1:
JsonReaderException: '{' is an invalid start of a property name. Expected a '"'. LineNumber: 2 | BytePositionInLine: 4
It began when I started to use VS Code instead of Visual Studio which I normally use. The project however can run successfully on another Windows machine that's only ever run Visual Studio. I've tried a few things and I'm out of ideas. Here are the steps I have tried:
- Confirmed with 2 other colleagues that
appsettings.json
,appsettings.Development.json
andlaunchSettings.json
do not have syntax errors as resolved in this thread - Confirmed they are all encoded with UTF-8 without BOM as suggested in this thread
- Closing Visual Studio 2019, deleting the .vs folder, opening and then running again
- Closing Visual Studio Code, deleting the .vscode folder, opening and then running again
- Switched branches and previous commits.
- Confirmed there is no user secrets configuration within
.csproj
- Deleting the repo files locally and cloning it again
- Restarting the PC entirely
- Creating a new templated ASP.NET Core Web Application within Visual Studio. It ran successfully.
- Confirmed that
args
value is a string array of size [1]. The [0] position is used and contains the string%LAUNCHER_ARGS%
. The environment variable does not appear inapp.config
or within Windows. I confirmedargs
is still the same and functions with the test pure template project.
Solution 1:[1]
Not to sound like a broken record, but anyone facing this issue, please ensure that your appsettings.json is properly formatted.
I found that in my case, escape characters were not properly set in my connection string. Instead of having .\\MSSQLSERVER, I had .\MSSQLSERVER.
If you have a test instance on your local machine, kindly take out the connection string for the cloud and replace it with that of the local machine (which should be simpler) and that should lead you in the right direction.
Solution 2:[2]
I had the same problem in starting a webApi on the same version of Asp.net Core. The problem was missing a curlybrace in appsettings.json
Solution 3:[3]
I got this error recently and the top comments weren't that helpful at solving it. So first of all this error 100% related to a JSON file not being formatted correctly. However the top comments don't really tell you which files you should examine to find the error.
A list of files you need to check to make sure it works:
appsettings.[environment-build]
(This is likely the source in 99% of cases)serviceDependencies.[area].json
Secrets.json
launchSettings.json
Appsettings
is found in your project directory.
LaunchSettings
and service Dependencies is found in your Properties folder.
Secrets file
is best found by going to Connected Services
(Note this applies to Visual Studio).
For my case my Secrets file was causing the issue. Note this isn't a complete list for every project ever made, but it should at least point you in the right direction instead of spending a few hours quadruple-checking your app settings files.
Solution 4:[4]
I had a similar issue with the same exception i.e. JsonReaderException: '}' is invalid after a single JSON value. Expected end of data. LineNumber: 13 | BytePositionInLine: 0.
When I examined appsettings.json file, I noticed a squiggly on the "}". This is where the problem was. Turned out that I had an extra "}" in my appsettings.json file. Some might have a missing curly brace. Rest assured, the problem lies here.
Solution 5:[5]
I had an issue with a value in my appsettings.json
that contained a path where the backslashes in the path were interpreted as escape characters.
Changing c:\local-export-folder
to c:\\local-export-folder
(double backslash to escape the backslash) did the trick for me.
I saw that you also have backslashes in your file. Maybe try to escape them.
Solution 6:[6]
Yes it is working once appsettings.json
file properly updated as:
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
Solution 7:[7]
In my case, it was AWS settings which were enclosed after installing AWS nuget. Look out for something similar to below AWS configuration in your appSettings.*.json file:
AWS nuget added following code:
{
"AWS": {
"Profile": "local-test-profile",
"Region": "ap-southeast-1"
}
},
whereas, it should be
"AWS": {
"Profile": "local-test-profile",
"Region": "ap-southeast-1"
},
Solution 8:[8]
Try to validate your json file through sites such us: https://jsonlint.com/ it helps you find the error easily.
Solution 9:[9]
In my case the cause of the error was in the appsettings.json file. I had forgotten to add a comma to separate my two ConnectionStrings.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | websplee |
Solution 2 | Washington Cavalcante |
Solution 3 | lejlun |
Solution 4 | user3610542 |
Solution 5 | |
Solution 6 | Doj |
Solution 7 | Jag |
Solution 8 | MJ X |
Solution 9 | Mxaza |