Could not parse the JSON file ,Error in Progam.cs asp.net core

50,133

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)

enter image description here

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

Share:
50,133
Runan Ngô
Author by

Runan Ngô

Updated on August 13, 2022

Comments

  • Runan Ngô
    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
      Mihail Stancescu over 6 years
      What's the JSON you want to parse?
    • Runan Ngô
      Runan Ngô over 6 years
      I don't know , I just wanna run project .NET core and system throw exception like this Does you have any idea to fix ?
    • NtFreX
      NtFreX over 6 years
      Probably you add configuration files in the Startup class. Take a look there.
    • Runan Ngô
      Runan Ngô over 6 years
      I checked debug in startup file but it throw exception before pass startup file
    • Palle Due
      Palle Due over 6 years
      Probably the file is missing, since the error is on line 0.
    • Runan Ngô
      Runan Ngô over 6 years
      I have resolve it ,thanks guys
    • adova
      adova over 6 years
      @Runan Ngô Can you share how you resolved it?
    • crthompson
      crthompson over 6 years
      Yes, please share your answer, I'm having the same problem.
    • Frode Nilsen
      Frode Nilsen almost 6 years
      An empty file gives this problem, it has to consist of {} at the least.
    • Jay Patel
      Jay Patel over 5 years
      The 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
    The_Butcher over 5 years
    what a legend!!! I migrated to AWS Secret Manager and just cleared out my secrets file.....
  • ignotus
    ignotus about 5 years
    Ah, thanks. I've just cleared out my secrets.json. Leaving a pair of curly braces {} solved it.
  • Shanteshwar Inde
    Shanteshwar Inde almost 5 years
    While this is not an answer, Please use comment section to ask additional or supplementary question. Thanks.
  • geisterfurz007
    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
    Jay almost 5 years
    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.
  • Blake
    Blake over 4 years
    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.
  • jhenninger
    jhenninger about 4 years
    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.
  • Shane
    Shane over 3 years
    I have appsettings.Test.json file and it was empty. The error disappeared after I added empty {} to the JSON settings file. Thank you!
  • binaryDi
    binaryDi over 3 years
    This is my case.
  • vullnetyy
    vullnetyy over 3 years
    Amazing. The same exact thing happened to me. <3 u guys and stackoverflow
  • MoxxiManagarm
    MoxxiManagarm over 3 years
    Please provide quality answers until you can write comments. This is not enough for an answer.
  • Sam Alekseev
    Sam Alekseev almost 3 years
    In my case i had empty appsettings.production.json file. Thank you.
  • Basil
    Basil over 2 years
    that was the solution in my case :) thanks
  • Raymond A.
    Raymond A. almost 2 years
    I made a console app and crashed into this error. In short: Thanks! Made an empty appsettings.json, I was only using local.settings.json.