Does System.Text.Json Deserialize into DataTable?

Khushi Shaikh 86 Reputation points
2020-08-13T21:11:57.34+00:00

Hello .Net Gurus - I have checked MS Documentation and I couldn't find any example of serialize json data using System.Text.JSON into DataTable. I have .Net Framwork 4.7.2 and did install the System.Text.Json NuGet package. The desrialization is working perfectly using NewtonSoft's JSON. I prefer to use Microsoft's libraries. Any guidance is much appreciated.

//Calling Code

 dt = JsonSerializer.Deserialize<DataTable>(apiResponse, new JsonSerializerOptions
             {
                 PropertyNameCaseInsensitive = true
             });

PS: While goggling I came across the post where a comment was "System.Text.JSON" doesn't support deserialize into DataTable. Is this true?

Thanks & Regards,

Khushi

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,922 questions
0 comments No comments
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,636 Reputation points
    2020-08-14T07:53:29.587+00:00

    Hi Khushisshaikh,
    I did some research about System.Text.Json and found that System.Text.Json has more restrictions.
    For example, there is currently no built-in support for types like DataSet and DataTable in System.Text.Json.
    And the official document says, there may be solutions for unsupported functions. The solution is to customize converters, they may not provide exactly the same functions as Newtonsoft.Json.
    More details you can refer to this document.
    So if you want to deserialize json to datatable, Newtonsoft.Json is a good choice.
    Here is a code example you can refer to.17589-example.txt
    Best Regards,
    Daniel Zhang

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Sławek 0 Reputation points
    2025-01-28T16:59:01.05+00:00

    Sorry may bad english. I had error in:

    DataSet data = JsonConvert.DeserializeObject<DataSet>(response.Content!)!;
    

    but advise from Daniel (that one in his file which I've referred to) works. I am happy.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.