Could not parse the JSON file ,Error in Progam.cs asp.net core
Solution 1
Is your appSettings.json
file (or whatever config you are using) formatted properly? WebHost.CreateDefaultBuilder
will not be able to parse it correctly if there are invalid characters at the start. I saw this issue when a copy/paste added a space at the beginning of the file.
Solution 2
I had a a similar problem as this in another thread and posted my solution over there: Application Startup Failure with Json read Error. Posting it here as well in case it disappears.
I came across the same problem. In my case, I'd started implementing app secrets but left it halfway through. My secrets.json
file was left linked but with invalid JSON.
Check your .csproj
to see if a <UserSecretId>
property is set under <PropertyGroup>
. If it's set, BuildWebHost()
will look through your secrets.json
file in '%APPDATA%\Microsoft\UserSecrets\{secretId}'
, in addition to your appsettings.json
file. An error in either file will cause the method to fail, but it won't tell you which file it is.
The solutions in my case were either to remove the <UserSecretId>
property or
Solution 3
There is a possibility it might happen when appsettings.json is not properly formated
In my case, I had the below configuration and got the error
{
"ConnectionStrings": {
"TransferDBConnection":
"Server=INGBTCPIC5DT04D;Database=TransferDB;Trusted_Connection=true;
},
***{***
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
ideally, it should be one extra { cause this problem
{
"ConnectionStrings": {
"TransferDBConnection":
"Server=INGBTCPIC5DT04D;Database=TransferDB;Trusted_Connection=true;"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}
Solution 4
Same problem and solved by removing <UserSecretsId> </UserSecretsId>
from *.cproj
.
It was a bad user secret configuration
Solution 5
I had the same issue with one of my complex API's, sadly I've wasted few hours to find the issue.
First of all check whether your appsettings.json
is well formatted as a standard JSON.
My issue was I've mistakenly typed some characters in the appsettings.json file like this, ( for me, 'ttings' characters cause this problem)
Also check whether all your json
files are not empty. A standard json
should at least have two curly braces.
Runan Ngô
Updated on August 13, 2022Comments
-
Runan Ngô over 1 year
I have some issues with the program.cs file, with the version of ASP.NetCORE 2.0
Here's my code
public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseContentRoot(Directory.GetCurrentDirectory()) .UseIISIntegration() .UseKestrel() .UseStartup<Startup>() .UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build()) .Build(); }
When I run the project, following error occurs.
System.FormatException: 'Could not parse the JSON file. Error on line number '0': ''.'
How to resolve it?
-
Mihail Stancescu over 6 yearsWhat's the JSON you want to parse?
-
Runan Ngô over 6 yearsI don't know , I just wanna run project .NET core and system throw exception like this Does you have any idea to fix ?
-
NtFreX over 6 yearsProbably you add configuration files in the Startup class. Take a look there.
-
Runan Ngô over 6 yearsI checked debug in startup file but it throw exception before pass startup file
-
Palle Due over 6 yearsProbably the file is missing, since the error is on line 0.
-
Runan Ngô over 6 yearsI have resolve it ,thanks guys
-
adova over 6 years@Runan Ngô Can you share how you resolved it?
-
crthompson over 6 yearsYes, please share your answer, I'm having the same problem.
-
Frode Nilsen almost 6 yearsAn empty file gives this problem, it has to consist of {} at the least.
-
Jay Patel over 5 yearsThe answer is that your
secrets.json
file must have been totally empty. There should at least be opening and closing braces as in{}
or else it will throw error as it will not be a valid json file if empty. I wasted too much time too but finally figured it out, thanks to comment of @FrodeNilsen comment above
-
-
The_Butcher over 5 yearswhat a legend!!! I migrated to AWS Secret Manager and just cleared out my secrets file.....
-
ignotus about 5 yearsAh, thanks. I've just cleared out my secrets.json. Leaving a pair of curly braces
{}
solved it. -
Shanteshwar Inde almost 5 yearsWhile this is not an answer, Please use comment section to ask additional or supplementary question. Thanks.
-
geisterfurz007 almost 5 years@ShanteshwarInde I disagree. The answer explains that copying the file from somewhere else appears to solve the problem which is a valid answer.
-
Jay almost 5 yearsI also had a problem with my appsettings.json. Or more speciflcally, with my appsettings.production.json, which overlays the appsettings.json for the production build of my software. Earlier, my appsettings.production.json had curly braces with nothing in them as content. I had accidentally removed those curly braces, thinking an empty file would work too. It didn't. I didn't find out until I was deploying, because my appsettings.development.json did have correct content. Lesson learned: have empty braces in your overlaying appsettings files.
-
Blake over 4 yearsThank you Jay! I messed things up when creating new environments and their appsettings.{environmentName}.json files were empty files. Added the curly braces to each file and it worked like a charm. Thank you again.
-
jhenninger about 4 yearsThis was a great lead for my issue. Somehow the secrets.json file was blank for the failing project. My solution was to add
{}
to my secrets.json file. To edit the file, right-click on the project within the Solution Explorer and select Manage User Secrets. -
Shane over 3 yearsI have appsettings.Test.json file and it was empty. The error disappeared after I added empty {} to the JSON settings file. Thank you!
-
binaryDi over 3 yearsThis is my case.
-
vullnetyy over 3 yearsAmazing. The same exact thing happened to me. <3 u guys and stackoverflow
-
MoxxiManagarm over 3 yearsPlease provide quality answers until you can write comments. This is not enough for an answer.
-
Sam Alekseev almost 3 yearsIn my case i had empty appsettings.production.json file. Thank you.
-
Basil over 2 yearsthat was the solution in my case :) thanks
-
Raymond A. almost 2 yearsI made a console app and crashed into this error. In short: Thanks! Made an empty appsettings.json, I was only using local.settings.json.