Home > Net >  Error: System.Text.Json.JsonException: 'N' is an invalid start of a value. Path: $ | LineN
Error: System.Text.Json.JsonException: 'N' is an invalid start of a value. Path: $ | LineN

Time:10-27

I have successfully created a rest API to connected to a Microsoft SQL database. All get, post, pull & delete functions work successfully in the API. I then created a UI using the Blazor server app. The UI communicates with the API to retrieve & send data. All info in the database have been retrieved successfully but I encounter this error "Error: System.Text.Json.JsonException: 'N' is an invalid start of a value. Path: $ | LineNumber: 0 | " when trying to add new data (task) to the database. When I reload the page after the error has occurred, the data is added successfully even though it fails in the initial process.

Below is the Create click...


@code{
    public class TasksClass
    {
        public int TaskID { get; set; }
        public string TaskTitle { get; set; }
        public string TaskDetails { get; set; }
        public string DateAdded { get; set; }
    }

    private IEnumerable<TasksClass> tasks = Array.Empty<TasksClass>();

    private string modalTitle;
    private int TaskID;
    private string TaskTitle;
    private string TaskDetails;
    private DateTime DateAdded;

// Create click
    private async Task createClick()
    {
        try
        {
            var tskObj = new TasksClass()
            {
                TaskTitle = TaskTitle,
                TaskDetails = TaskDetails,
                DateAdded = DateAdded.ToString("yyyy-MM-dd")
            };

            var request = new HttpRequestMessage(HttpMethod.Post,
                config["API_URL"]   "Tasks/AddTasks");

            request.Content = new StringContent(JsonSerializer.Serialize(tskObj), null, "application/json-patch json");

            var client = ClientFactory.CreateClient();

            var response = await client.SendAsync(request);

            using var responseStream = await response.Content.ReadAsStreamAsync();

            string res = await JsonSerializer.DeserializeAsync<string>(responseStream);

            await JS.InvokeVoidAsync("alert", res);

            await refreshList();
        }
        catch(HttpRequestException)
        {
            throw new BadHttpRequestException("no");
        }
        

        }


I put a breakpoint in the createClick & did a walk through & realized the error occurs here

string res = await JsonSerializer.DeserializeAsync(responseStream);

        await JS.InvokeVoidAsync("alert", res);

I can't seem to figure out exactly what the problem is.

CodePudding user response:

That error is being thrown because the response body starts with an "N", but the JsonDeserializer is expecting it to start as a JSON object (e.g. with a curly brace). My assumption is that Tasks/AddTasks is just returning a string, so it's not in JSON format. Quick solution is to just not deserialize the response, because all you want is the raw string response anyway.

To do so, just replace these lines:

using var responseStream = await response.Content.ReadAsStreamAsync()    
string res = await JsonSerializer.DeserializeAsync<string>(responseStream);

with this:

string res = await response.Content.ReadAsStringAsync();
  • Related