Click here to Skip to main content
15,037,928 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.
   
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);
Code Fan 28-Jul-21 19:58pm
   
Remove ="", please. You can't have that in type specs. Make sure you can compile first.
Mandavali 28-Jul-21 11:18am
   
I don't know why object="" is showing there no object="" in line it is just object
Code Fan 28-Jul-21 14:17pm
   
Maybe you want to post your DataContract classes here. Objects cannot be serialized properly without KnownTypes. It's more a JSON contract design issue here.
Mandavali 2-Aug-21 11:19am
   
public class Attributes
{
public string name { get; set; }

[JsonProperty("time-zone")]
public string TimeZone { get; set; }
public string timezone { get; set; }
public string locale { get; set; }
public string address { get; set; }

[JsonProperty("address-line-one")]
public string AddressLineOne { get; set; }

[JsonProperty("address-line-two")]
public string AddressLineTwo { get; set; }
public string city { get; set; }
public string state { get; set; }
public string country { get; set; }
public string zip { get; set; }
public double longitude { get; set; }
public double latitude { get; set; }

[JsonProperty("full-name")]
public string FullName { get; set; }
public string email { get; set; }
public object host { get; set; }

[JsonProperty("host-email")]
public object HostEmail { get; set; }

[JsonProperty("user-data")]
public List<userdata> UserData { get; set; }

[JsonProperty("email-status")]
public string EmailStatus { get; set; }
}
public class Data
{
public string id { get; set; }
public string type { get; set; }
}

public class Company
{
public Data data { get; set; }
public string id { get; set; }
public string type { get; set; }
public Attributes attributes { get; set; }
}
public class UserData
{
public string field { get; set; }
public string value { get; set; }
}
public class Payload
{
public string id { get; set; }
public string type { get; set; }
public Attributes attributes { get; set; }
public Relationships relationships { get; set; }
}

public class Root
{
public string @event { get; set; }
public Meta meta { get; set; }
public Payload payload { get; set; }
}
Mandavali 2-Aug-21 11:22am
   
I want to save data only from user data section
"user-data": [
{
"field": "Name",
"value": "Some Name"
},
{
"field": "Visit",
"value": "Visitor"
},
{
"field": "Email Address",
"value": "email@email"
}
{
"field": "Phone",
"value": "Phonenumber"
}

]
Code Fan 2-Aug-21 13:19pm
   
Your JSON objects seem fine, as strings are always OK. Now, may you post your ExecuteAsync code again? Make sure it compiles first. When declaring variables, try use the "var" keyword. Thanks!

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