Problem with JSONItem not being an array

  1. 7 days ago

    Dean D

    Aug 12 Pre-Release Testers, Xojo Pro Santa Ynez California

    I am trying to sync two databases using JSONItems. The JSON string below is what is returned from the server.

    {"ZuluTime":"2019-08-12 23:42:34","rSetups":{"FacCode":"20466805","LastSync":"2019-08-12 23:42:34"}}

    dim data as new JSONItem
    data = js,Lookup("rSetups", nil)
    if data <>nil then
        for j as integer = 0 to data.Count-1 
            app.adb.SQLExecute "UPDATE Setups SET Item = '" + data.Value(j) + "', TimeStamp = '" + ts + "' WHERE Item = '" + data.Name(j) + "'"
        next
    end if

    My code throws a JSONExceeption saying "JSONItem is not an array". This is a very small test and there could be many more items. How can I get it to be treated as an array instead of an object?

    data shows as {"FacCode":"20466805","LastSync":"2019-08-12 23:20:30"} in the debugger

    Thanks all for your input. Understanding it better, I was able to use the Append method to create an array in the routine that sends the JSON string.

  2. Steve U

    Aug 12 Pre-Release Testers, Xojo Pro Seattle, WA

    tough to say

    the problem is that the JSON item is truly not an array.

    Arrays are available in JSON and look like: { "myArray": [ {"item1":"value1"},{"item2":"value2"},{"item3":"value3"}] }

    (note the square brackets)

    So it sounds like you're treating it like an array when you should treat it otherwise. Check out the structure of the JSONitem in the debugger. You might need to navigate from child to child in your processing loop rather than treat it as an array

  3. Bob K

    Aug 12 Pre-Release Testers, Xojo Pro Kansas City

    Because it's not data that you need in your loop, you need the JSONItem from the Child. Also you need to get the value from the JSONItem. You may want to use Lookup instead because if "Item" is not in the list row it will throw an exception.

    Something like this (off the top of my head):

    if data <>nil then
        for j as integer = 0 to data.Count-1 
            dim oChild as JSONItem = data.child(j)
    
            app.adb.SQLExecute "UPDATE Setups SET TimeStamp = '" + ts + "' WHERE Item = '" + oChild.value("Item") + "'"
        next
    end if
  4. Wayne G

    Aug 12 Pre-Release Testers, Xojo Pro New Zealand axisdirect.nz

    It is probably just a typo here, but I notice you have a comma between js & lookup rather than a stop. I would expect this to fail when building?

  5. 6 days ago

    Kem T

    Aug 13 Pre-Release Testers, Xojo Pro, XDC Speakers Connecticut

    To build on Bob's reply, "{}" denotes an object (the equivalent of a Xojo Dictionary), whereas "[]" denotes an array. Your JSON is an object within an object.

  6. Dean D

    Aug 13 Pre-Release Testers, Xojo Pro Answer Santa Ynez California

    Thanks all for your input. Understanding it better, I was able to use the Append method to create an array in the routine that sends the JSON string.

or Sign Up to reply!