1

I'm trying to fetch information about orders from an API for CDON, but I've ran into a problem when I'm trying to iterate through the product rows, and I can't figure out what is causing it.

I'm using json2.asp and here is the code I'm using.

strCallback = getCDONOrder(CDONorderID)
response.write strCallback & "<br><br>" 'This outputs the Json below for reference
set CDONObj = json.parse(strCallback)
for each orderProduct in CDONObj.OrderDetails.OrderRows
    response.write orderProduct & "<br>"
next

The strCallback contains correct json-formatted code and the output looks like this (slightly simplified for easier overviewing):

{
    "OrderDetails": {
        "CustomerInfo": {
            "CustomerId": 0,
            "ShippingAddress": {
                "Name": "Firstname Lastname",
                "StreetAddress": "Streetname, 15",
                "ZipCode": "12345",
                "City": "Cityname",
                "Country": "SE"
            }
        },
        "OrderRows": [{
            "OrderRowId": 1,
            "FulfillmentStatus": "Invoiced",
            "PaymentStatus": "AwaitingPayment",
            "ProductId": "30761620",
            "ProductName": "Test-Product",
            "ProductType": "Article",
            "Quantity": 1,
            "PricePerUnit": 3995.0
        }],
        "TotalVat": 799.0
    }
}

I am able to fetch the quantity with orderProduct.Quantity on the first iteration, however the For Each-loop doesn't stop after the first iteration, despite there only being one product. If I try to grab it within the loop it will crash on the second iteration through the loop. If I run the code above it will keep going and create this output:

[object Object]
function(k) { return this[k]; }
function(k,v) { if(typeof(v) === "unknown") { try { v = (new VBArray(v)).toArray(); } catch(e) { return; } } this[k] = v; }
function(k) { delete this[k]; }
function() { var d = new ActiveXObject("Scripting.Dictionary"); for(var key in this) { if(this.hasOwnProperty(key)) { d.add(key, this[key]); } } return d.keys(); }

Now, the first [object Object] is correct, and contains the product order row we want to look at, but the following rows I don't know where it comes from and why it keeps iterating over them. The code in those 4 rows are from the json2.asp-file.

I have tried putting if isObject(orderProduct) then within the loop, but it will still output those rows.

What is happening here, and how can I solve it?

EDIT: Dirty fix to circumvent the problem. I managed to find an if-statement to put around the inside of the for each loop. It doesn't stop it from looping, but it does stop it from crashing when trying to fetch the Quantity (and other values) within the loop.

for each orderProduct in CDONObj.OrderDetails.OrderRows
    if orderProduct.get("Quantity")<>"" then
        ...
    end if
next

I'm not going to put it down as an answer as it doesn't answer the question about why it's happening, but perhaps the edit can help someone else in a similar situation.

Daniel Nordh
  • 362
  • 3
  • 15
  • Unfortunately not. Nothing in there describes what I'm getting, and it's where I got the idea to try `isObject()` from, which didn't work. – Daniel Nordh May 12 '21 at 09:33
  • 1
    No, I'm suggesting you use [aspjson](https://www.aspjson.com/) instead of [json2](https://github.com/nagaozen/asp-xtreme-evolution/blob/master/lib/axe/classes/Parsers/json2.asp) as it's not well supported and examples are few and far between. See [Any good libraries for parsing JSON in Classic ASP?](https://stackoverflow.com/q/1019223). – user692942 May 12 '21 at 09:35
  • While it's not an answer to this question, it's worth looking into. I like the easier syntax of json2 and have used it successfully in other projects. Just not sure why it's giving me problems here. – Daniel Nordh May 12 '21 at 09:40
  • 1
    Might get a chance to test this, later on. I'll see what I can do. – user692942 May 12 '21 at 09:50
  • 2
    @user692942 I was able to circumvent the bug. Added it to my question. So at least it's "solved" for now for me, but in case you do find anything interesting it would be nice to have an answer as to why it's behaving the way it does. – Daniel Nordh May 12 '21 at 11:37
  • @DanielNordh Looking through the json2.asp file, you can use the `object.keys()` collection to iterate through. `for each key in CDONObj.OrderDetails.OrderRows.keys() : response.write CDONObj.OrderDetails.OrderRows.get(key).OrderRowId & "
    " : next`. The `.keys()` returns indexes of an array object , and names of a name-value collection.
    – Flakes May 13 '21 at 04:40
  • @DanielNordh in the linked json2.asp (which is different from the version I have), instead of `.keys()`, you have to use `.enumerate()`. – Flakes May 13 '21 at 05:06

0 Answers0