JSON Parsing Issue - 'S' is an invalid escapable character within a JSON
Asked Answered
L

2

7

I have the following JSON in appsettings file , it has a connection string.

 {
      "Logging": {
        "LogLevel": {
          "Default": "Warning"
        }
      },
      "ApplicationInsights": {
        "InstrumentationKey": "8eje88w0-1509-4ae0-b58c-1a7ee5b9c7e4"
      },
       "ConnectionStrings": {
        "DefaultConnString": "Server=MYPC\SQLEXPRESS;Database=CoreService.Mark;Trusted_Connection=True;"
      },
      "AllowedHosts": "*"
    }

But upon executing the program.I keep getting the following error.I tried to find any errors,but could not locate any.Please advice

Unhandled exception. System.FormatException: Could not parse the JSON file.
 ---> System.Text.Json.JsonReaderException: 'S' is an invalid escapable character within a JSON string. The string should be correctly escaped. LineNumber: 10 | BytePositionInLine: 42
Latinalatinate answered 16/3, 2021 at 7:35 Comment(3)
Back slashes must be escaped.Winfredwinfrey
either use the forward slash, or use the backslash twiceQuiz
Json string definition from Json.org: json.org/img/string.pngDissatisfaction
K
17

In your ConnectionStrings. DefaultConnString field, you're escaping S (before SQLExpress;). As the error suggests, you can't do that.

You can fix this by adding another escape character (backslash, \) in front of the pre-existing one. Backslashes are escapable characters and the end result will be the wanted one, you'll get one backslash in that position.

Korman answered 16/3, 2021 at 7:38 Comment(0)
S
1

write this:

.\\SQLEXPRESS

you must write double backslash

Shelby answered 8/8, 2022 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.