Avatar of Peter Nordberg
Peter Nordberg
Flag for Sweden asked on

Deserialize

Hi,

I need to deserialize the following JSON code that comes to a specified url on my asp.net website (I'm using vb.net). The serialized code I would like to add to an object so I can add each element to the database.

[{"email":"john.doe@sendgrid.com","timestamp":1337197600,"smtp-id":"<4FB4041F.6080505@sendgrid.com>","event":"processed"},{"email":"john.doe@sendgrid.com","timestamp":1337966815,"smtp-id":"<4FBFC0DD.5040601@sendgrid.com>","category":"newuser","event":"processed"},{"email":"john.doe@sendgrid.com","timestamp":1337969592,"smtp-id":"<20120525181309.C1A9B40405B3@Mohammeds-Mac.local>","event":"processed"},{"email":"bill.nye@thescienceguy.com","timestamp":1337971272,"smtp-id":"<20120525184109.9578F40409A5@Mohammeds-Mac.local>","reason":"Unsubscribed Address","event":"dropped"},{"email":"bill.nye@thescienceguy.com","timestamp":1337979680,"smtp-id":"<20120525210118.7F3724041B6D@Mohammeds-Mac.local>","reason":"Unsubscribed Address","event":"dropped"},{"email":"bill.nye@thescienceguy.com","timestamp":1337980426,"smtp-id":"<20120525211344.2844C4041EB2@Mohammeds-Mac.local>","reason":"Unsubscribed Address","event":"dropped"},{"email":"iamemail@gmail.com","attempt":"17","smtp-id":"<4FB29F5D.5080404@sendgrid.com>","timestamp":1337201407,"response":"501 Syntax error - Badly formatted address. ","event":"deferred"},{"email":"iamemail@gmail.com","attempt":"18","smtp-id":"<4FB29F5D.5080404@sendgrid.com>","timestamp":1337212247,"response":"Connection timed out","event":"deferred"},{"email":"iamemail@gmail.com","attempt":"25","smtp-id":"<4FB29F5D.5080404@sendgrid.com>","timestamp":1337288707,"response":"501 Syntax error - Badly formatted address. ","event":"deferred"},{"email":"john.doe@sendgrid.com","smtp-id":"<4FBFC0DD.5040601@sendgrid.com>","timestamp":1337966817,"response":"250 2.0.0 OK 1337966817 z9si3846029icy.10 ","category":"newuser","event":"delivered"},{"email":"john.doe@sendgrid.com","smtp-id":"<20120525183816.12AA6404092A@Mohammeds-Mac.local>","timestamp":1337971099,"response":"250 2.0.0 OK 1337971099 l10si4052001icz.31 ","event":"delivered"},{"email":"foobar@yahoo.com","smtp-id":"<20120525183951.59BEA4040968@Mohammeds-Mac.local>","timestamp":1337971198,"response":"250 2.0.0 OK 1337971198 xz10si2239282obb.108 ","event":"delivered"},{"email":"foobar@yahoo.com","smtp-id":"<20120525181029.220EE404043A@Mohammeds-Mac.local>","timestamp":1337969434,"status":"5.1.1","reason":"550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces. Learn more at http://support.google.com/mail/bin/answer.py?answer=6596 z6si25868142igk.15 ","type":"bounce","event":"bounce"},{"email":"john.doe@sendgrid.com","timestamp":1338574236,"url":"http://www.sendgrid.com","event":"click"},{"email":"john.doe@sendgrid.com","timestamp":1338572978,"url":"http://www.sendgrid.com","event":"click"},{"email":"john.doe@sendgrid.com","timestamp":1338595596,"url":"http://www.sendgrid.com","event":"click"},{"email":"john.doe@sendgrid.com","timestamp":1338312226,"newsletter":{"newsletter_user_list_id":"1695412","newsletter_id":"310318","newsletter_send_id":"425605"},"category":["dsfdsf","Newsletter"],"event":"open"},{"email":"john.doe@sendgrid.com","timestamp":1338487320,"event":"open"},{"email":"john.doe@sendgrid.com","timestamp":1338578578,"event":"open"},{"email":"john.doe@sendgrid.com","timestamp":1340126539,"newsletter":{"newsletter_user_list_id":"1695412","newsletter_send_id":"478915","newsletter_id":"428128"},"category":["TESTt","Newsletter"],"event":"unsubscribe"},{"email":"john.doe@sendgrid.com","timestamp":1338310756,"newsletter":{"newsletter_user_list_id":"1695100","newsletter_send_id":"425584","newsletter_id":"331537"},"category":["TEST","Newsletter"],"event":"unsubscribe"},{"email":"john.doe@sendgrid.com","timestamp":1340126539,"newsletter":{"newsletter_user_list_id":"1695412","newsletter_send_id":"478915","newsletter_id":"428128"},"category":["TESTt","Newsletter"],"event":"unsubscribe"},{"email":"mysendgriid@hotmail.com","event":"spamreport","timestamp":1347307636,"category":["board_collaborator_new_pin"],"unique_args":{"tracking_id":"c99z72cd5537515d9b0e3d8cab1ec09e","time_sent":1346814447,"sender_username":"notasendr","recipient_username":"realusername"}}]

Open in new window


Is there any of you experts that can show me how to accomplish this?!

Thanks for help!

Peter
ASP.NETXMLVisual Basic.NET

Avatar of undefined
Last Comment
Peter Nordberg

8/22/2022 - Mon
aikimark

If performance is an issue, I would recommend the fastJSON code from Mehdi Gholam:
http://fastjson.codeplex.com/

Author's description of the project and performance comparisons to other JSON deserializing functions/methods/libraries.
http://www.codeproject.com/Articles/159450/fastJSON
ASKER CERTIFIED SOLUTION
Bob Learned

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Peter Nordberg

ASKER
Hi and thanks so much for the code example,

How do I get the dll files for that library (saw it on Github, but do I need to download some special part?).

How do I actually bring in the url response to my class?

Thanks so much for your help!

Peter
Bob Learned

You can install NewtonSoft from Nuget:

Json.NET 5.0.8
http://www.nuget.org/packages/newtonsoft.json/
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Peter Nordberg

ASKER
Hi and thanks,

Did you see my other question?

Peter
Bob Learned

How do I actually bring in the url response to my class?
I would need more context before I could suggest something.  

What kind of application are you working with?  

How do you get the response?
Peter Nordberg

ASKER
asp.net site with vb.net
this is a description of the post:


Peter
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bob Learned

Description missing...
Peter Nordberg

ASKER
Bob Learned

It sounds like when SendGrid sends an email for you, that you get a notification as a post to a URL.

If you have a standard Web Forms ASP.NET web site, then you can read the post in the Page_Load event handler.  

Dim userName As String = Request.Form("UserName")
Dim password As String = Request.Form("Password")

Open in new window


If it is a different type of web site, then I would need to know what you are working with.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Peter Nordberg

ASKER
Hi,

They say it's an  HTTP Posts in JSON format. Can that be retrieved the way you described above?

Peter
Peter Nordberg

ASKER
it's sent to an url specified for example http://mysite.com/webhook.aspx. But then I need to desirialize it.

Peter
Peter Nordberg

ASKER
I can test to post to an url and then I get this example for what is posted:

JSON post
The JSON data posted is like this:
[{"email":"john.doe@sendgrid.com","timestamp":1337197600,"smtp-id":"<4FB4041F.6080505@sendgrid.com>","event":"processed"},{"email":"john.doe@sendgrid.com","timestamp":1337966815,"smtp-id":"<4FBFC0DD.5040601@sendgrid.com>","category":"newuser","event":"processed"},{"email":"john.doe@sendgrid.com","timestamp":1337969592,"smtp-id":"<20120525181309.C1A9B40405B3@Mohammeds-Mac.local>","event":"processed"},{"email":"bill.nye@thescienceguy.com","timestamp":1337971272,"smtp-id":"<20120525184109.9578F40409A5@Mohammeds-Mac.local>","reason":"Unsubscribed Address","event":"dropped"},{"email":"bill.nye@thescienceguy.com","timestamp":1337979680,"smtp-id":"<20120525210118.7F3724041B6D@Mohammeds-Mac.local>","reason":"Unsubscribed Address","event":"dropped"},{"email":"bill.nye@thescienceguy.com","timestamp":1337980426,"smtp-id":"<20120525211344.2844C4041EB2@Mohammeds-Mac.local>","reason":"Unsubscribed Address","event":"dropped"},{"email":"iamemail@gmail.com","attempt":"17","smtp-id":"<4FB29F5D.5080404@sendgrid.com>","timestamp":1337201407,"response":"501 Syntax error - Badly formatted address. ","event":"deferred"},{"email":"iamemail@gmail.com","attempt":"18","smtp-id":"<4FB29F5D.5080404@sendgrid.com>","timestamp":1337212247,"response":"Connection timed out","event":"deferred"},{"email":"iamemail@gmail.com","attempt":"25","smtp-id":"<4FB29F5D.5080404@sendgrid.com>","timestamp":1337288707,"response":"501 Syntax error - Badly formatted address. ","event":"deferred"},{"email":"john.doe@sendgrid.com","smtp-id":"<4FBFC0DD.5040601@sendgrid.com>","timestamp":1337966817,"response":"250 2.0.0 OK 1337966817 z9si3846029icy.10 ","category":"newuser","event":"delivered"},{"email":"john.doe@sendgrid.com","smtp-id":"<20120525183816.12AA6404092A@Mohammeds-Mac.local>","timestamp":1337971099,"response":"250 2.0.0 OK 1337971099 l10si4052001icz.31 ","event":"delivered"},{"email":"foobar@yahoo.com","smtp-id":"<20120525183951.59BEA4040968@Mohammeds-Mac.local>","timestamp":1337971198,"response":"250 2.0.0 OK 1337971198 xz10si2239282obb.108 ","event":"delivered"},{"email":"foobar@yahoo.com","smtp-id":"<20120525181029.220EE404043A@Mohammeds-Mac.local>","timestamp":1337969434,"status":"5.1.1","reason":"550 5.1.1 The email account that you tried to reach does not exist. Please try double-checking the recipient's email address for typos or unnecessary spaces. Learn more at http://support.google.com/mail/bin/answer.py?answer=6596 z6si25868142igk.15 ","type":"bounce","event":"bounce"},{"email":"john.doe@sendgrid.com","timestamp":1338574236,"url":"http://www.sendgrid.com","event":"click"},{"email":"john.doe@sendgrid.com","timestamp":1338572978,"url":"http://www.sendgrid.com","event":"click"},{"email":"john.doe@sendgrid.com","timestamp":1338595596,"url":"http://www.sendgrid.com","event":"click"},{"email":"john.doe@sendgrid.com","timestamp":1338312226,"newsletter":{"newsletter_user_list_id":"1695412","newsletter_id":"310318","newsletter_send_id":"425605"},"category":["dsfdsf","Newsletter"],"event":"open"},{"email":"john.doe@sendgrid.com","timestamp":1338487320,"event":"open"},{"email":"john.doe@sendgrid.com","timestamp":1338578578,"event":"open"},{"email":"john.doe@sendgrid.com","timestamp":1340126539,"newsletter":{"newsletter_user_list_id":"1695412","newsletter_send_id":"478915","newsletter_id":"428128"},"category":["TESTt","Newsletter"],"event":"unsubscribe"},{"email":"john.doe@sendgrid.com","timestamp":1338310756,"newsletter":{"newsletter_user_list_id":"1695100","newsletter_send_id":"425584","newsletter_id":"331537"},"category":["TEST","Newsletter"],"event":"unsubscribe"},{"email":"john.doe@sendgrid.com","timestamp":1340126539,"newsletter":{"newsletter_user_list_id":"1695412","newsletter_send_id":"478915","newsletter_id":"428128"},"category":["TESTt","Newsletter"],"event":"unsubscribe"},{"email":"mysendgriid@hotmail.com","event":"spamreport","timestamp":1347307636,"category":["board_collaborator_new_pin"],"unique_args":{"tracking_id":"c99z72cd5537515d9b0e3d8cab1ec09e","time_sent":1346814447,"sender_username":"notasendr","recipient_username":"realusername"}}]

Open in new window

⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Bob Learned

In the Webhook.aspx, you should be able to get posted JSON, but you need to know the key.  One possible way to discover that is to put a breakpoint in the Page_Load, and examine the Request.Form keys.  Another way is to use an HTTP debugger, like Fiddler.
Peter Nordberg

ASKER
On their page they said I can test the post by using this command:
curl -X POST -H "Content-Type: application/json" -d '[{"email":"john.doe@sendgrid.com","timestamp":1337197600,"smtp-id":"<4FB4041F.6080505@sendgrid.com>","event":"processed"},{"email":"john.doe@sendgrid.com","timestamp":1337966815,"smtp-id":"<4FBFC0DD.5040601@sendgrid.com>","category":"newuser","event":"clicked"},{"email":"john.doe@sendgrid.com","timestamp":1337969592,"smtp-id":"<20120525181309.C1A9B40405B3@Example-Mac.local>","event":"processed"}]'

Open in new window


Does that say something to you?

Peter
Bob Learned

CURL is a utility that you can download:

http://curl.haxx.se/download.html

It can simulate the JSON post from their service.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Peter Nordberg

ASKER
Hi,

Exuse me for not being active on this thread. I had to turn my attention to another acute problem. However, I have not been able to solve this yet. There is no "key" to get from this JSON response and I don't really know how to be able to see what data is sent to be so that I can begin to work with it.

Peter
Peter Nordberg

ASKER
Hi,

I've come this far in trying to get hold of the json post but I can't get it to work. Anyone that can give me some more direction on how to manage?!

 Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
          

            Request.InputStream.Position = 0 'you need this else the magic doesn't happen
            Dim inputStream As New StreamReader(Request.InputStream)
            Dim json As String = inputStream.ReadToEnd()

            'Dim nl As New Sendgrid
            Dim jss As New System.Web.Script.Serialization.JavaScriptSerializer()
            Dim dict As Dictionary(Of String, String) = jss.Deserialize(Of Dictionary(Of String, String))(json)
            'We're telling it with the String, String to expect both the name of the value and the value as strings.
            For Each item As KeyValuePair(Of String, String) In dict
                Response.Write(item.Key & " - " & item.Value & "<br>")
                If dict.ContainsKey("email") Then
                    'nl.Email = dict("email")
                    Insert(dict("email"))
                End If
            Next

            'Dim nl As SendgridJsonConvert.DeserializeObject(Of Sendgrid)(json)
            'Dim dict As Dictionary(Of String, String) =
            'Response.Write(nl.Email)


        End If
    End Sub


    Public Sub Insert(email As String)
        Dim dbConn As New SqlConnection(config.ConnectionString)
        Dim sqlString As String
        sqlString = "INSERT INTO testSendgrid (email) VALUES (@email)"

        Dim sqlCmd As New SqlCommand(sqlString, dbConn)
        sqlCmd.CommandType = CommandType.Text

        sqlCmd.Parameters.AddWithValue("@email", email)

        Try

            dbConn.Open()

            sqlCmd.ExecuteNonQuery()

        Finally
            dbConn.Close()
            sqlCmd = Nothing
            dbConn = Nothing
        End Try

    End Sub

Open in new window