Click here to Skip to main content
15,886,067 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am just begginer in creating a webhook. I really need help .. Please let me know..
I started writing a project in visual studio 2019 , a webhook receiver..
downloaded Nuget package Microsoft.AspNet.WebHooks.Receiver and WebAPI
Created a WebApiConfig.class with Register Method
and a created another class with CustomWebHookHandler

Payload I receive is a Json. how do I save that payload to SQL database..

What I have tried:

I am just begginer in creating a webhook. I really need help .. Please let me know..
I started writing a project in visual studio 2019 , a webhook receiver..
downloaded Nuget package Microsoft.AspNet.WebHooks.Receiver and WebAPI
Created a WebApiConfig.class with Register Method
and a created another class with CustomWebHookHandler

Payload I receive is a Json. how do I save that payload to SQL database..
Posted
Updated 28-Jul-21 4:48am

1 solution

You can always save it as is in nvarchar(max). When you need it again for processing, you can deserialize it from a string, mapped from nvarchar(max). If you want to be able to query or index it, you'll have to decompose it into a proper SQL schema or use SQL graphs, available after SQL Server 2017.

Just to add that you can serialize a JSON object into XML as well, which can be stored in SQL as XML, a feature available after SQL Server 2005, I believe. I don't recommend it though, as everyone is deprecating XML. I don't know when Microsoft will drop the support for XML in SQL.
 
Share this answer
 
v2
Comments
Mandavali 28-Jul-21 11:11am    
This is my public override Task ExecuteAsync(string generator, WebHookHandlerContext context)
{
// Get data from WebHook
CustomNotifications data = context.GetDataOrDefault<customnotifications>();

// Get data from each notification in this WebHook
foreach (IDictionary<string, object=""> notification in data.Notifications)

{
// Process data
IDictionary<string, object=""> keyValues = JsonConvert.DeserializeObject<idictionary<string, object="">>(data);



}

return Task.FromResult(true);
}
}

in the line IDictionary<string, object=""> keyValues = JsonConvert.DeserializeObject<idictionary<string, object="">>(data);
>>(data) gives me error cannot convert from custom notifications to string
Code Fan 28-Jul-21 13:07pm    
Why deserialize the variable "data" in a loop? In your case, your JSON object has been deserialized, no need to deserialize it again. Run your code in debug mode, and watch the variable "data". Browse through its structure & code against it accordingly.
Mandavali 28-Jul-21 15:59pm    
Thank you so much I am learning and very new to Json and webhook.
in foreach loop now I should be able access it by property correct..
string strName = Data.["Name"] and at that point I should setup a database connection and supply the values correct.
Code Fan 28-Jul-21 19:55pm    
Of course, CustomNotifications is a JSON class already deserialized for you by the time you access it in this method, no need to deserialize anymore.
Mandavali 28-Jul-21 11:17am    
sorry the line is IDictionary<string, object=""> keyValues = JsonConvert.DeserializeObject<idictionary<string, object="">>(data);

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



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