Click here to Skip to main content
16,020,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi, I am getting this error in Blazor

JsonSerializationException: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'DTO' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.
Path '', line 1, position 1.

Here is my API Code

C#
//var patientDiagnosis = await _context.PatientDiagnosis.FirstOrDefaultAsync(e=>e.Pid==Pid);
 var patientDiagnosis = await _context.PatientDiagnosis.Where(e => e.Pid == Pid).ToListAsync();
                
                if (patientDiagnosis == null)
                {
                    logger.LogWarning($"Record Not Found:{nameof(GetPatientDiagnosis)}-ID:{Pid}");
                    return NotFound();
                }
                return Ok(patientDiagnosis);

If I use FirstOrDefaultAsync API to show data in a Blazor form, but if I use Where then it shows the above error. But in Swagger or Postman API it is working fine.

Anybody have any idea please?

What I have tried:

I have no idea how to solve this in a Blazor app because it works in Postman and the error shows only when I use

C#
await _context.PatientDiagnosis.Where(e => e.Pid == Pid).ToListAsync();
Posted
Updated 26-Jan-22 6:08am
v3

1 solution

Your API client is expecting your endpoint to return a single record.

You cannot change the endpoint to return a list of records and expect it to magically work.

Either add a different endpoint for returning the list of records, or change your client code so that it deserializes the response as a list of records instead of a single record.
 
Share this answer
 
Comments
Member 13291196 26-Jan-22 10:48am    
Thank you richard for your valuable response. can you please suggest any idea? i use Nswag Studio for Generate Client code
Richard Deeming 26-Jan-22 11:28am    
I have already told you what you need to do - either add a new endpoint to return the list of records, or update your client code to deserialize the response from the existing endpoint correctly.
Member 13291196 26-Jan-22 11:34am    
yeah, I create another Endpoint from the Controller side. but from client-side its showing the same error
Richard Deeming 26-Jan-22 11:37am    
Then either your initial endpoint is still returning a list when your client expects it to return a single record, or your client is expecting the new endpoint to return a single record.

The client needs to match the endpoint - either the endpoint returns a single record, and the client deserializes a single record; or the endpoint returns a list of records, and the client deserializes a list of records.

As I keep trying to explain to you, the error from your question is what you get when the endpoint returns a list of records, and the client expects a single record.
Member 13291196 26-Jan-22 11:42am    
Got It Richard Thankyou

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900