C
C#2y ago
Lewis C

❔ Send API response to a SQL Server Table

I have a basic console app that does a post request on a API and prints the returning json to the console. I’d like to “import” this into an existing table on a MS SQL Server database. Is this possible. I’ve tried some examples out there but they don’t work for my example. Any help is appreciated
6 Replies
Becquerel
Becquerel2y ago
yes, this is possible - you'll first have to take the JSON and 'deserialize' it into an in-memory object then send that in-memory object to your database with a db provider such as ADO.NET (low-level), Dapper (medium-level), or Entity Framework (high-level)
Becquerel
Becquerel2y ago
you can use https://json2csharp.com/ to generate a class definition for your json
Convert JSON to C# Classes Online - Json2CSharp Toolkit
Convert any JSON object to C# classes online. Json2CSharp is a free toolkit that will help you generate C# classes on the fly.
Becquerel
Becquerel2y ago
then System.Text.Json.JsonSerializer.Deserialize<YourGeneratedType>(yourJsonString) Dapper is fairly simple to use and lets you execute raw SQL, if you're comfortable with it
Lewis C
Lewis C2y ago
It’s currently stored as a string so in the (yourJsonString) is just put the name of the string
Becquerel
Becquerel2y ago
that's correct
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.