I'm new to webhooks and json but have been searching this site and the rest of google trying to figure out how to process a sendgrid json post for a project I'm working on. I have the code working functionally but get either nothing/empty string returned for my data or an error (for unique args).
I'm using newtsonsoft.json and currently just posting to an aspx page and trying to output the data to tets the process. I have a fixed sample json post that I'm sending.
All info below - would really appreciate someone advising me where I've screwed up please as I've been searching for samples and solutions and am stuck!
Code
Public Class sgData
Public Property sgEmail() As String
Public Property sgTimestamp() As String
Public Property sgSMTPID() As String
Public Property sgEvent() As String
Public Property sgCategory() As String
Public Property sgEventID() As String
Public Property sgMsgID() As String
Public Property sgUserAgent() As String
Public Property sgIP() As String
Public Property sgURL() As String
Public Property sgASMGroup() As String
Public Property sgUniqueArgs As uArgs
End Class
Public Class uArgs
Public Property CB() As String
Public Property emailType() As String
Public Property prodGroup() As String
End Class
Public Class xhandler
Inherits System.Web.UI.Page
Implements System.Web.IHttpHandler
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim whReader As New StreamReader(HttpContext.Current.Request.InputStream)
Dim strjson = whReader.ReadToEnd()
Dim sgResponse = JsonConvert.DeserializeObject(Of List(Of sgData))(strjson)
For Each item In sgResponse
HttpContext.Current.Response.Write("-> event=" & item.sgEvent & Chr(13)) ' is empty/nothing
HttpContext.Current.Response.Write("-> CB=" & item.sgUniqueArgs.CB & Chr(13)) ' throws an error = Object reference not set to an instance of an object
Next
End Sub
End Class
And the json content...
[
{
"email": "example@test.com",
"timestamp": 1517928354,
"smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
"event": "processed",
"category": "cat facts",
"sg_event_id": "uMaJLSrLHH96LFwwpCju5w==",
"sg_message_id": "14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0",
"unique_args": {
"CB": "111",
"emailType": "invite",
"prodGroup": "REFEX"
}
},
{
"email": "example@test.com",
"timestamp": 1517928354,
"smtp-id": "<14c5d75ce93.dfd.64b469@ismtpd-555>",
"event": "deferred",
"category": "cat facts",
"sg_event_id": "3pSLStZrkwJ52nPCBa6z-Q==",
"sg_message_id": "14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0",
"response": "400 try again later",
"attempt": "5",
"unique_args": {
"CB": "222",
"emailType": "invite",
"prodGroup": "REFEX"
}
}
]
Just use a JSON library like Json.NET to parse your string. Maybe this could help you.
OK sorted it. Just deserialized it directly into an object and then deserialized the unique_args into a dictionary to lop through, completely ignoring the classes I created.
Working code snippet below:
Dim sgResponse = JsonConvert.DeserializeObject(strjson)
For Each item In sgResponse
sgEventType = item("event").ToString
candEmail = item("email").ToString
Dim uniqueArgs = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(item("unique_args").ToString)
CB = uniqueArgs("CB")
emailType = uniqueArgs("emailType")
prodGroup = uniqueArgs("prodGroup")
HttpContext.Current.Response.Write("-> event=" & sgEventType & ", email=" & candEmail & ", CB=" & CB & ", emailType=" & emailType & ", prodGroup=" & prodGroup & Chr(13))
Next
And it outputs the following:
-> event=processed, email=example@test.com, CB=111, emailType=invite, prodGroup=REFEX
-> event=deferred, email=example@test.com, CB=222, emailType=invite, prodGroup=REFEX
thx, just couldn't get my head around it earlier for some reason :-D
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.