Need help with listing JSON data in listbox

I’ve been at this for a couple days and not sure what to do and where I am going wrong. I have a desktop app communicating with a Xojo Cloud web service. The part I am stuck on is when I need to get multiple results from the server. I am not sure if my problem is in the web service’s method of returning the data or the desktop app parsing out the individual bits, or most likely both

Here is a snippet of the JSON data retrieved. I’m not sure if this even looks correct as I’ve seen other posts with JSON data having straight brackets [ … ], but mine doesn’t

{"Assignments":{"29":{"AssignID":"29","AssignmentName":"Kristie's assignment"},"19":{"AssignID":"19","AssignmentName":"Ryan’s assignment"},"20":{"AssignID":"20","AssignmentName":"Tom’s assignment"}}}

And here it is formatted:

{
    "Assignments":
    {
        "29":
        {
            "AssignID":"29",
            "AssignmentName":"Kristie's assignment"
        },
        "19":
        {
            "AssignID":"19",
            "AssignmentName":"Ryan’s assignment"
        },
        "20":
        {
            "AssignID":"20",
            "AssignmentName":"Tom’s assignment"
        }
    }
}

I want to list in a 2-column listbox the AssignID and the AssignmentName. I’ve tried several approaches I found in the forum but nothing is working for me

The straight brackets in JSON have a meaning, they mean that the contents are an array. The JSON you’ve provided, each item is a named node “19”, “20”, and “29”. If you want to have an array of items instead, you would use JSONItem.Add when building the JSONItem in code.

You could still fill a Listbox with the JSON you’ve provided, you would just use the .Keys function to check each named key on “Assignments”.

Did you need help changing the way your web app sends the JSON so that it sends an array?

References:
JSON website - the spec and details
Xojo JSONItem Documentation

//src is your json string source
Var jsrc As New JSONItem(src)
If not jsrc.HasKey("Assignments") then Return //there is not an Assignments key

Var j As JSONItem=jsrc.Value("Assignments")

Var jk() As String=j.Keys
For Each k As String In jk
  Var jv As JSONItem=j.Value(k)
  If jv.HasKey("AssignID") And jv.HasKey("AssignmentName") then //check for AssignID and AssignmentName keys
    Var id As String=jv.Value("AssignID")
    Var name As String=jv.Value("AssignmentName")
    If Not id.IsEmpty And Not name.IsEmpty Then
      ListBox1.AddRow id, name
    End If
  End If
Next

Ah! That’s good to know. JSON is still really new to me, so I have a lot yet to learn

I think since my JSON is technically not incorrect, I think I will stick with the way the data is coming back as a non-array. Plus, Antonio posted with some code, and it worked!

You are a gem Antonio! Thank you for this!