Runtime: ASP.net Core 3.1 Could not parse the JSON file

Created on 8 Mar 2020  路  11Comments  路  Source: dotnet/runtime

I have started the upgrade of my project from ASP.net core 2.1 to 3.1, but having an issue with reading the appsettings.json file. This file has not changed and I am getting the following error:

'Could not parse the JSON file.'
JsonReaderException: '0x10' is invalid within a JSON string. The string should be correctly escaped. LineNumber: 8 | BytePositionInLine: 21.

This is not an issue in other ASP.net core 3.1 projects that I have created and the following is format of my appsettings.json:

{
"DataProtection": {
"ApplicationName": "test",
"BlobName": "test",
"SasToken": "test",
"ContainerName": "test",
"StorageUrl": "https://test.windows.net"
},
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning",
"Microsoft": "Warning",
"System": "Warning"
},
"AllowedHosts": "*"
}
}

Please could you help with this issue

area-Extensions-Configuration needs more info

All 11 comments

The problem is the the url you have in your json file. The json serializer has changed in dotnet core 3 and it apparently needs escaping of forward slashes (which is not required for json format). For a quick workaround try reverting to using Json.Net by adding its nuget package and setting it up like this:

services.AddControllers().AddNewtonsoftJson()

Thanks for your reply. I have amended the url to the following, but still the same error:

"DataProtection": {
"ApplicationName": "test",
"BlobName": "test",
"SasToken": "test",
"ContainerName": "test",
"StorageUrl": "https://test.windows.net"
}

I have also tried removing this section of code entirely, but same error again. Also, I have tried the line of code you have provided in your response and changing the format of the .json file to UTF8 from UTF8-BOM, but still no luck

The problem is the the url you have in your json file. The json serializer has changed in dotnet core 3 and it apparently needs escaping of forward slashes (which is not required for json format). For a quick workaround try reverting to using Json.Net by adding its nuget package and setting it up like this:

While that's true, it has nothing to do with this particular issue and that sample code only fixes MVC.

@mjthompson2107

I have also tried removing this section of code entirely, but same error again. Also, I have tried the line of code you have provided in your response and changing the format of the .json file to UTF8 from UTF8-BOM, but still no luck

Can you make a github repo with the problematic application?

I cannot upload the whole application, as there is a lot of sensitive code in the app, but I have attached the program and startup files. Thanks for your help so far. Please could you see if there is anything that could cause the error from these files
Program.txt
Startup.txt

Can you please update your exact appsettings.json file? I want to make sure we don't lose something with encoding in the copy/paste to comments. /cc @layomia

I have a similar problem. here is my full appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "ABCD_TEST": "Data Source=ab-cdefgh-12.1234567x1abc.database.windows.net;Initial Catalog=ABCD-TEST;Persist Security Info=True;User ID=abc_user;Password=A5X_ab#r%^"
  }
}

So in my case, I had at one point used the Manage User Secrets option in Visual Studio which created an empty secrets.json file. That file is the one that was giving me problems as it was empty and that is not valid json. I added {} into that file and the JSON parse error disappears.

So in my case, I had at one point used the Manage User Secrets option in Visual Studio which created an empty secrets.json file. That file is the one that was giving me problems as it was empty and that is not valid json. I added {} into that file and the JSON parse error disappears.

This resolved my issues. Solution > Right click to Manage User Secrets and Open the file in explore if the file is empty.

@uchagani and @Shiva-netmasons It sounds like this was caused by feeding an empty secrets.json in both cases. I don't think we should make AddJsonFile tolerate an empty file as it's an indication that something is wrong with the input.

I think these are both different issues than what @mjthompson2107 is seeing, since his error is clearly calling out line/position number.

@mjthompson2107 we really need the actual file (or a similar file that reproduces the issue but doesn't contain sensitive information) to help you. Please do not copy-paste content of the file, but drag/drop into the issue (as a .zip if GitHub doesn't like the extension).

@mjthompson2107 we really need the actual file (or a similar file that reproduces the issue but doesn't contain sensitive information) to help you. Please do not copy-paste content of the file, but drag/drop into the issue (as a .zip if GitHub doesn't like the extension).

@mjthompson2107 could you please provide more info to repro this issue?

my issue has been resolved now. same solution as @uchagani

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bencz picture bencz  路  3Comments

iCodeWebApps picture iCodeWebApps  路  3Comments

omajid picture omajid  路  3Comments

jzabroski picture jzabroski  路  3Comments

nalywa picture nalywa  路  3Comments