1

I am trying to loop through some basic JSON and for some reason, I am getting a a Overflow: 'Clng' error when loading the JSON and cannot figure out why. Any help would be super appreciated, thanks.

I am using the EXACT version 1.19 code from here: https://github.com/gerritvankuipers/aspjson/blob/master/aspJSON1.19.asp (error happens on line 265 of the script)

Here is the JSON: (it's valid JSON - I tested on https://jsonlint.com/)

strJSON  = "[{""id"":29599,""title"":""Lucky Voice"",""category_id"":45,""sub_category_ids"":[],""timestamp"":""2015-07-01T13:06:31.851013Z"",""start_time"":""2015-07-01T17:00:00+01:00"",""end_time"":""2021-07-01T03:00:00+01:00"",""door_time"":null,""last_entry_time"":null,""summary"":""Lucky Voice is the most liberating, heart-racing, life-affirming private karaoke experience on Earth. But don't just take our word for it..."",""description"":""Lucky Voice - simply the best! Hire out one of our karaoke rooms for you and your friends and experience some of the best fun around. Whether you are 12 or 92 and love singing, then this is the place for you! Plus, you wont be disturbed by anyone, unless of course, you press the \""thirsty\"" button for a drink...\r\n\r\nOpening Hours:\r\nMon - Wed 5:30pm - 1:00am\r\nThurs - Sat 5:30pm - 3:00am\r\nSun 3:00pm - 10:30pm"",""organiser_name"":""Lucky Voice"",""hashtag"":""luckyvoice"",""image_url"":""https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/4626090b-9c8d-484d-92e6-c0331d4af9cd/soho3.jpg"",""image_urls"":[""https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/4626090b-9c8d-484d-92e6-c0331d4af9cd/soho3.jpg"",""https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/7199c9d4-cfe2-44e9-9523-a1d9e9a78e0d/Brighton%20Pod2.jpg""],""links"":{""Website"":""http://go-staging.evvnt.com/29599-0"",""Booking"":""http://go-staging.evvnt.com/29599-1"",""Facebook"":""http://go-staging.evvnt.com/29599-2"",""Twitter"":""http://go-staging.evvnt.com/29599-3"",""YouTube"":""http://go-staging.evvnt.com/29599-4""},""artists"":"""",""keywords"":""karaoke, lucky voice, soho"",""prices"":{""Average pp"":""GBP 22""},""workflow_state"":""report_complete"",""capacity"":null,""venue"":{""id"":16838,""name"":""Lucky Voice"",""address_1"":""52 Poland Street"",""address_2"":"""",""town"":""London"",""country"":""GB"",""postcode"":""W1F 7NQ"",""latitude"":51.5146901,""longitude"":-0.136974},""contact"":{""name"":""Contact Name 26421"",""email"":""contact26421@example.com"",""tel"":""123456789""}},{""id"":46658,""title"":""Base SAS Online Training at VirtualNuggets.com"",""category_id"":124,""sub_category_ids"":[],""timestamp"":""2016-02-10T05:19:39.666806Z"",""start_time"":""2016-02-12T01:00:00+00:00"",""end_time"":""2024-08-17T23:00:00+01:00"",""door_time"":null,""last_entry_time"":null,""summary"":""VirtualNuggets is the one of Best organization providing Online and Corporate Training worldwide."",""description"":""Base SAS provides a scalable, included software environment specially designed for data access, alteration and reporting. It includes a fourth-generation programming language; ready-to-use programs for data handling, information storage and recovery, evocative statistics and report writing; and an influential macro facility that reduces programming time and preservation headaches.\r\n\r\nVirtualNuggets is the one of Best organization providing Online and Corporate Training worldwide. We offer Real-time practical Oriented Training on Base SAS, New batch starts on every week.\r\n\r\nFor more information on Base SAS Online Training\r\n\r\nEmail\t        :  info@virtualnuggets com\r\nContact       :  +1-707 666 8949(USA)\t  +91-888 556 0202(India)\r\nWeb URL    :  http://www.virtualnuggets.com/base-sas.html\r\nBlogger       : http://basesasonlinetraining.blogspot.com\r\n"",""organiser_name"":""vasubuddi"",""hashtag"":""basesas"",""image_url"":""https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/a49c513e-f8ce-4095-b51d-989e7d8e3d75/VirtualNuggets.png"",""image_urls"":[""https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/a49c513e-f8ce-4095-b51d-989e7d8e3d75/VirtualNuggets.png""],""links"":{""Website"":""http://go-staging.evvnt.com/46658-0"",""Inquiries"":""http://go-staging.evvnt.com/46658-1""},""artists"":"""",""keywords"":""base sas, sas, online training, virtualnuggets"",""prices"":{""Training"":""USD 0""},""workflow_state"":""reported_on"",""capacity"":null,""venue"":{""id"":26319,""name"":""station road"",""address_1"":""station road"",""address_2"":"""",""town"":""Sandwell"",""country"":""GB"",""postcode"":""B71 4HJ"",""latitude"":52.5273431,""longitude"":-1.9883300652359},""contact"":{""name"":""Contact Name 43585"",""email"":""contact43585@example.com"",""tel"":""123456789""}}]"

Here is the Processing Code:

    Set oJSON = New aspJSON

    'Wrap with events to turn it into a collection so we can loop below (i commented this out, i get the same error regardless though).
    'From here: https://stackoverflow.com/questions/30782144/looping-through-json-using-aspjson
    strJSON = "{""events"":" & strJSON & "}"

    'Load JSON string
    oJSON.loadJSON(strJSON)

Once we get the JSON loading, I cannot get this to print out the data from each record - I get :

    'Loop through each item in collection
    For Each oEvent In oJSON.data("events")
        'Errors Here: Object required: '[string: "29599"]'
        Set this = oJSON.data("events").item(oEvent)
        Response.Write this.item("id") & ": " & this.item("title") & "<br>"
        
        For Each link In this.item("links")
            Response.Write link & ":" & this.item("links").item(link) & "<br>"
        Next
    Next

What I need to do is print out / get access to each ID (29599 & 46658) as well as all the data under it. There are two records here, but there could be 1000s returned.

Here is the formatted valid JSON string:

[{
    "id": 29599,
    "title": "Lucky Voice",
    "category_id": 45,
    "sub_category_ids": [],
    "timestamp": "2015-07-01T13:06:31.851013Z",
    "start_time": "2015-07-01T17:00:00+01:00",
    "end_time": "2021-07-01T03:00:00+01:00",
    "door_time": null,
    "last_entry_time": null,
    "summary": "Lucky Voice is the most liberating, heart-racing, life-affirming private karaoke experience on Earth. But don't just take our word for it...",
    "description": "Lucky Voice - simply the best! Hire out one of our karaoke rooms for you and your friends and experience some of the best fun around. Whether you are 12 or 92 and love singing, then this is the place for you! Plus, you wont be disturbed by anyone, unless of course, you press the \"thirsty\" button for a drink...\r\n\r\nOpening Hours:\r\nMon - Wed 5:30pm - 1:00am\r\nThurs - Sat 5:30pm - 3:00am\r\nSun 3:00pm - 10:30pm",
    "organiser_name": "Lucky Voice",
    "hashtag": "luckyvoice",
    "image_url": "https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/4626090b-9c8d-484d-92e6-c0331d4af9cd/soho3.jpg",
    "image_urls": ["https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/4626090b-9c8d-484d-92e6-c0331d4af9cd/soho3.jpg", "https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/7199c9d4-cfe2-44e9-9523-a1d9e9a78e0d/Brighton%20Pod2.jpg"],
    "links": {
        "Website": "http://go-staging.evvnt.com/29599-0",
        "Booking": "http://go-staging.evvnt.com/29599-1",
        "Facebook": "http://go-staging.evvnt.com/29599-2",
        "Twitter": "http://go-staging.evvnt.com/29599-3",
        "YouTube": "http://go-staging.evvnt.com/29599-4"
    },
    "artists": "",
    "keywords": "karaoke, lucky voice, soho",
    "prices": {
        "Average pp": "GBP 22"
    },
    "workflow_state": "report_complete",
    "capacity": null,
    "venue": {
        "id": 16838,
        "name": "Lucky Voice",
        "address_1": "52 Poland Street",
        "address_2": "",
        "town": "London",
        "country": "GB",
        "postcode": "W1F 7NQ",
        "latitude": 51.5146901,
        "longitude": -0.136974
    },
    "contact": {
        "name": "Contact Name 26421",
        "email": "contact26421@example.com",
        "tel": "123456789"
    }
}, {
    "id": 46658,
    "title": "Base SAS Online Training at VirtualNuggets.com",
    "category_id": 124,
    "sub_category_ids": [],
    "timestamp": "2016-02-10T05:19:39.666806Z",
    "start_time": "2016-02-12T01:00:00+00:00",
    "end_time": "2024-08-17T23:00:00+01:00",
    "door_time": null,
    "last_entry_time": null,
    "summary": "VirtualNuggets is the one of Best organization providing Online and Corporate Training worldwide.",
    "description": "Base SAS provides a scalable, included software environment specially designed for data access, alteration and reporting. It includes a fourth-generation programming language; ready-to-use programs for data handling, information storage and recovery, evocative statistics and report writing; and an influential macro facility that reduces programming time and preservation headaches.\r\n\r\nVirtualNuggets is the one of Best organization providing Online and Corporate Training worldwide. We offer Real-time practical Oriented Training on Base SAS, New batch starts on every week.\r\n\r\nFor more information on Base SAS Online Training\r\n\r\nEmail\t        :  info@virtualnuggets com\r\nContact       :  +1-707 666 8949(USA)\t  +91-888 556 0202(India)\r\nWeb URL    :  http://www.virtualnuggets.com/base-sas.html\r\nBlogger       : http://basesasonlinetraining.blogspot.com\r\n",
    "organiser_name": "vasubuddi",
    "hashtag": "basesas",
    "image_url": "https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/a49c513e-f8ce-4095-b51d-989e7d8e3d75/VirtualNuggets.png",
    "image_urls": ["https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/a49c513e-f8ce-4095-b51d-989e7d8e3d75/VirtualNuggets.png"],
    "links": {
        "Website": "http://go-staging.evvnt.com/46658-0",
        "Inquiries": "http://go-staging.evvnt.com/46658-1"
    },
    "artists": "",
    "keywords": "base sas, sas, online training, virtualnuggets",
    "prices": {
        "Training": "USD 0"
    },
    "workflow_state": "reported_on",
    "capacity": null,
    "venue": {
        "id": 26319,
        "name": "station road",
        "address_1": "station road",
        "address_2": "",
        "town": "Sandwell",
        "country": "GB",
        "postcode": "B71 4HJ",
        "latitude": 52.5273431,
        "longitude": -1.9883300652359
    },
    "contact": {
        "name": "Contact Name 43585",
        "email": "contact43585@example.com",
        "tel": "123456789"
    }
}]

*** UPDATE ***

The JSON code was missing [ ] around it, it now will get through the first iteration and errors with Object required: 'oJSON.data(...).Item(...)' once it hits the Set this = oJSON.data("events").item(oEvent) the second time. I cleaned up the code above, This is what I printed the first iteration:

29599: Lucky Voice
Website:http://go-staging.evvnt.com/29599-0
Booking:http://go-staging.evvnt.com/29599-1
Facebook:http://go-staging.evvnt.com/29599-2
Twitter:http://go-staging.evvnt.com/29599-3
YouTube:http://go-staging.evvnt.com/29599-4

ERROR: Object required: 'oJSON.data(...).Item(...)'
Dennis
  • 708
  • 2
  • 10
  • 25
  • That’s a lot of JSON in a VBScript string could you edit the question and add the RAW JSON (not a VBScript string) so we can review it. In it’s current form is very difficult to read. – user692942 Jun 02 '21 at 20:33
  • Added above at the bottom. you can validate it here: https://jsonlint.com – Dennis Jun 03 '21 at 01:42
  • 2
    The error is because of overflow when the numbers in your json have too many digits after the decimal point,(eg: "longitude": -1.9883300652359). In `aj_ReadNumericValue` function within the aspjson file that is linked, the code tries to cast this value to long _after_ removing the decimal point, resulting in the overflow. Find `val = Clng(Replace(val, ".", ""))` in the linked aspjson file, and change to `val = CDbl(Replace(val, ".", ""))` . – Flakes Jun 03 '21 at 03:27
  • That seems to have worked. The new issue I have - how do you loop through multiple levels with aspJSON? there are no examples of this on their site: https://github.com/gerritvankuipers/aspjson (they are single level) You will see above in the formatted JSON that there are two records - id 29599 and id 46658. I spent over an hour and there are no examples (that I could find) on google either, which is very odd! Thanks for the help. – Dennis Jun 03 '21 at 05:12
  • With your current code, `For Each link In this.item("links") : Response.Write link & ":" & this.item("links").item(link) & "
    " : Next` within the `Each oEvent In oJSON.data("events")` loop should give you the links.
    – Flakes Jun 03 '21 at 05:25
  • It does not work. I added to the question above the error and the exact code. It errors with `Object required: '[string: "29599"]'` at the `Set this =` – Dennis Jun 03 '21 at 12:11
  • It has to have something to do with the JSON string. I just tested this solution and it works fine. I see two results. https://stackoverflow.com/questions/30782144/looping-through-json-using-aspjson But when I run mine, it only iterates and sees ONE result... – Dennis Jun 03 '21 at 13:26
  • Bit short sighted of them to use `CLng()` instead of `CDbl()` good catch @Flakes – user692942 Jun 03 '21 at 16:31
  • 1
    totally agree - how many people use this?! – Dennis Jun 03 '21 at 17:45

1 Answers1

1

Here is the answer to this. Issue was related to the JSON and a bracket missing - UGH! Hopes this helps someone.

  1. the CLNG issue was resolved by Flakes suggestion in comments - thank you.

  2. Loop example - here is full code.

    strJSON  = "{""id"":29599,""title"":""Lucky Voice"",""category_id"":45,""sub_category_ids"":[],""timestamp"":""2015-07-01T13:06:31.851013Z"",""start_time"":""2015-07-01T17:00:00+01:00"",""end_time"":""2021-07-01T03:00:00+01:00"",""door_time"":null,""last_entry_time"":null,""summary"":""Lucky Voice is the most liberating, heart-racing, life-affirming private karaoke experience on Earth. But don't just take our word for it..."",""description"":""Lucky Voice - simply the best! Hire out one of our karaoke rooms for you and your friends and experience some of the best fun around. Whether you are 12 or 92 and love singing, then this is the place for you! Plus, you wont be disturbed by anyone, unless of course, you press the \""thirsty\"" button for a drink...\r\n\r\nOpening Hours:\r\nMon - Wed 5:30pm - 1:00am\r\nThurs - Sat 5:30pm - 3:00am\r\nSun 3:00pm - 10:30pm"",""organiser_name"":""Lucky Voice"",""hashtag"":""luckyvoice"",""image_url"":""https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/4626090b-9c8d-484d-92e6-c0331d4af9cd/soho3.jpg"",""image_urls"":[""https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/4626090b-9c8d-484d-92e6-c0331d4af9cd/soho3.jpg"",""https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/7199c9d4-cfe2-44e9-9523-a1d9e9a78e0d/Brighton%20Pod2.jpg""],""links"":{""Website"":""http://go-staging.evvnt.com/29599-0"",""Booking"":""http://go-staging.evvnt.com/29599-1"",""Facebook"":""http://go-staging.evvnt.com/29599-2"",""Twitter"":""http://go-staging.evvnt.com/29599-3"",""YouTube"":""http://go-staging.evvnt.com/29599-4""},""artists"":"""",""keywords"":""karaoke, lucky voice, soho"",""prices"":{""Average pp"":""GBP 22""},""workflow_state"":""report_complete"",""capacity"":null,""venue"":{""id"":16838,""name"":""Lucky Voice"",""address_1"":""52 Poland Street"",""address_2"":"""",""town"":""London"",""country"":""GB"",""postcode"":""W1F 7NQ"",""latitude"":51.5146901,""longitude"":-0.136974},""contact"":{""name"":""Contact Name 26421"",""email"":""contact26421@example.com"",""tel"":""123456789""}},{""id"":46658,""title"":""Base SAS Online Training at VirtualNuggets.com"",""category_id"":124,""sub_category_ids"":[],""timestamp"":""2016-02-10T05:19:39.666806Z"",""start_time"":""2016-02-12T01:00:00+00:00"",""end_time"":""2024-08-17T23:00:00+01:00"",""door_time"":null,""last_entry_time"":null,""summary"":""VirtualNuggets is the one of Best organization providing Online and Corporate Training worldwide."",""description"":""Base SAS provides a scalable, included software environment specially designed for data access, alteration and reporting. It includes a fourth-generation programming language; ready-to-use programs for data handling, information storage and recovery, evocative statistics and report writing; and an influential macro facility that reduces programming time and preservation headaches.\r\n\r\nVirtualNuggets is the one of Best organization providing Online and Corporate Training worldwide. We offer Real-time practical Oriented Training on Base SAS, New batch starts on every week.\r\n\r\nFor more information on Base SAS Online Training\r\n\r\nEmail\t        :  info@virtualnuggets com\r\nContact       :  +1-707 666 8949(USA)\t  +91-888 556 0202(India)\r\nWeb URL    :  http://www.virtualnuggets.com/base-sas.html\r\nBlogger       : http://basesasonlinetraining.blogspot.com\r\n"",""organiser_name"":""vasubuddi"",""hashtag"":""basesas"",""image_url"":""https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/a49c513e-f8ce-4095-b51d-989e7d8e3d75/VirtualNuggets.png"",""image_urls"":[""https://s3-eu-west-1.amazonaws.com/evvnt.staging/uploads/event_image/event_image/a49c513e-f8ce-4095-b51d-989e7d8e3d75/VirtualNuggets.png""],""links"":{""Website"":""http://go-staging.evvnt.com/46658-0"",""Inquiries"":""http://go-staging.evvnt.com/46658-1""},""artists"":"""",""keywords"":""base sas, sas, online training, virtualnuggets"",""prices"":{""Training"":""USD 0""},""workflow_state"":""reported_on"",""capacity"":null,""venue"":{""id"":26319,""name"":""station road"",""address_1"":""station road"",""address_2"":"""",""town"":""Sandwell"",""country"":""GB"",""postcode"":""B71 4HJ"",""latitude"":52.5273431,""longitude"":-1.9883300652359},""contact"":{""name"":""Contact Name 43585"",""email"":""contact43585@example.com"",""tel"":""123456789""}}"

    'Wrap with "events" to turn it into a collection
    strJSON = "{""events"":[" & strJSON & "]}"

    Set oJSON = New aspJSON

    'Load JSON string
    oJSON.loadJSON(strJSON)

    'Loop through each record in the collection
    i = 0
    For Each eventrec In oJSON.data("events")
        
        'Shorthand access to each record
        Set this = oJSON.data("events").item(eventrec)

        'Access the base level items
        Response.Write i & ") " & this.item("id") & " = " & this.item("title") & "<br>"
        
        'Get Images
        bStop = False
        c = 0
        Do Until bStop
            If Len(this.item("image_urls").item(c)) > 0 Then
                Response.Write this.item("image_urls").item(c) & "<br>"
            Else
                bStop = True
            End If
            c = c + 1
        Loop
                
        'Links
        For Each link In this.item("links")
            Response.Write link & ":" & this.item("links").item(link) & "<br>"
        Next

        'Venue
        For Each link In this.item("venue")
            Response.Write link & ":" & this.item("venue").item(link) & "<br>"
        Next

        i = i + 1
    Next
user692942
  • 16,398
  • 7
  • 76
  • 175
Dennis
  • 708
  • 2
  • 10
  • 25