26

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?

MrMalith
  • 1,352
  • 14
  • 22
Runan Ngô
  • 261
  • 1
  • 3
  • 4

15 Answers15

36

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.

ijb109
  • 942
  • 1
  • 19
  • 30
29

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

Steepmountain
  • 363
  • 3
  • 7
14

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": "*"
}
6

Same problem and solved by removing <UserSecretsId> </UserSecretsId> from *.cproj.

It was a bad user secret configuration

nuhkoca
  • 1,777
  • 4
  • 20
  • 44
habib salimi
  • 116
  • 2
  • 8
  • This 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. – jhenninger Apr 20 '20 at 21:37
6

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)

enter image description here

Also check whether all your json files are not empty. A standard json should at least have two curly braces.

MrMalith
  • 1,352
  • 14
  • 22
4

I had this problem and the reason is that for some reason Visual Studio saves files with some invisible metadata that goes before the text therefore JSON reader cannot parse it.

You need to File -> Save As -> (Choose UTF-8 Without BOM).

Stan
  • 25,744
  • 53
  • 164
  • 242
4

In my case I had faced this issue as I had added User Secret to my solution and later removed all of its content including { }. Please make sure the content in the file is with proper JSON format. Or try providing an empty object { } to the file if no information needed from it.

2

I had the same problem.
I found that this problem seemed to have been caused by my appsettings.json file.
I copied a version from another place to solve this problem.

geisterfurz007
  • 5,292
  • 5
  • 33
  • 54
Jason
  • 143
  • 1
  • 5
  • While this is not an answer, Please use comment section to ask additional or supplementary question. Thanks. – Shanteshwar Inde May 08 '19 at 10:43
  • 1
    @ShanteshwarInde I disagree. The answer explains that copying the file from somewhere else appears to solve the problem which is a valid answer. – geisterfurz007 May 09 '19 at 18:18
  • 1
    I 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. – Jay May 20 '19 at 23:19
  • Thank 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. – Blake Oct 16 '19 at 12:16
2

in m case I put extra { right before (above) "ConnectionStrings" : in appsettings.json

I copied this from another file and assumed that "ConnectionStrings" : must inside a curly braces {}

before

...
},
{  //<-- this one the culprit
    "ConnectionStrings": {
      "Dapper": "Server=.;Database=Dapper;Trusted_Connection=True;"
    },

after

...
},
    "ConnectionStrings": {
      "Dapper": "Server=.;Database=Dapper;Trusted_Connection=True;"
    },
rtfmpliz
  • 159
  • 1
  • 6
0

Check if you don't have any hidden spaces or tabs in your json. You can use https://jsonlint.com/ to verify your json format.

Enrico
  • 2,734
  • 1
  • 27
  • 40
0

Check your appsettings.json file. that problem is because your file doesn't have a comma,brackets, Curly brackets or another punctuation sign.

0

enter image description here

This issue could come if your appsetting.json is not well-formatted.

In my case somehow I'd an unwanted character in my appsettings.json

If you see carefully in sdk section I've got an extra l that only was causing the exception.

{
    "AppSettings": {
        "DefaultConnection": "ProdConnection",
        "DoGoogleAnalytics": "false"
    }, 
    "sdk": {l
        "version": "1.0.0"
    },
    "ConnectionStrings": {
        "LocalConnection": "data source=DATA_SERVER\\SQLEXPRESS,12345;initial catalog=DATABASE_NAME;persist security info=True;user id=db_user_id;password=db_password;MultipleActiveResultSets=true",
    }
}
Ashish Tripathi
  • 580
  • 4
  • 18
0

I had the same problem. The issues was a missing comma ',' after ConnectionString curly brackets in my appsettings.json. My appsettings.json was not properly formatted. So I only had to add the comma and that was it.

"ConnectionStrings": { "DefaultConnection": "Server=(local); Integrated Security= true; Database = PharmDataDB; MultipleActiveResultSet=true;" }

Sanele
  • 11
  • 4
-1

for me, it was missing comma in appsettings.json

-3

Just place ConnectionStrings in appsetting file then run the migration command.

Murtaza
  • 1
  • 1