2

I've come this far with help from http://www.aspjson.com/ and Looping through JSON Array using ASPJSON

<!--#include file="aspjson.asp" -->

jsonstring = "{""events"":""[{
""email"":""john@test.com"",
""timestamp"":1461015166,
""smtp-id"":""\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e"",
""event"":""processed"",
""category"":""cat facts"",
""sg_event_id"":""Y4SkYnMgWCH-j0Ap2HUBlg=="",
""sg_message_id"":""14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0""
},
{
"email"":""example@test.com"",
""timestamp"":1461015166,
""smtp-id"":""\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e"",
""event"":""deferred"",
""category"":""cat facts"",
""sg_event_id"":""QVvoFUd9LS5083KTavaqAw=="",
""sg_message_id"":""14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0"",
""response"":""400 try again later"",""attempt"":""5""
},
{
""email"":""john@test.com"",
""timestamp"":1461015166,
""smtp-id"":""\u003c14c5d75ce93.dfd.64b469@ismtpd-555\u003e"",
""event"":""processed"",
""category"":[""Cat Facts"",""Fish Facts""],
""sg_event_id"":""Y4SkYnMgWCH-j0Ap2HUBlg=="",
""sg_message_id"":""14c5d75ce93.dfd.64b469.filter0001.16648.5515E0B88.0""
}]""}"


Set jJSON = New aspJSON
jJSON.loadJSON(jsonstring)

For Each son In jJSON.data("events")
For Each grandson In jJSON.data("events").item(son)
    response.write "<b>" & grandson & ": </b>" & jJSON.data("events").item(son).item(grandson) & "<br>"
Next
response.write "<br>"
Next

Most of the examples I've found have the names of the keys but I don't always know what they are.

My problem comes when my loop gets to the last set where the category contains two elements. "Cat Facts" and "Fish Facts".

error message is: Microsoft VBScript runtime error '800a01c2' Wrong number of arguments or invalid property assignment

If I could get this working it would be a step in the right direction, but "Categories" could be any name in another record and it could have single or multiple values.

I think I need to add an if/then but I'm stuck here. I think I want to do something like this pseudo code

if isarray(grandson) then
for each greatgrandson in grandson
    response.write greatgrandson
else
    response.write "<b>" & grandson & ": </b>" & jJSON.data("events").item(son).item(grandson) & "<br>"
end if

I'm sure its evident that I'm still struggling with how all this fits together, so if anyone has any pointers or links to descriptions that might be helpful it would be much appreciated.

Community
  • 1
  • 1
slamslam102
  • 63
  • 2
  • 8

1 Answers1

2

You could check if the grandson is an object, and if it is loop through it an display:

For Each son In jJSON.data("events")
For Each grandson In jJSON.data("events").item(son)
    response.write "<b>" & grandson & ": </b>"
    if NOT isObject(jJSON.data("events").item(son).item(grandson)) then
        response.write jJSON.data("events").item(son).item(grandson) 
    else
        for each category in jJSON.data("events").item(son).item(grandson)
            Response.write jJSON.data("events").item(son).item(grandson)(category) & ","
        next
    end if
    Response.write "<br>"
Next
response.write "<br>"
Next
Flakes
  • 2,422
  • 8
  • 28
  • 32