Parsing JSON, Stumped by Array of Objects that are Dictionaries

I’m using @Kem_Tekinay’s terrific M_JSON classes to parse some incoming JSON that includes a dictionary and several arrays of objects, all of which are dictionaries. Maybe it’s just one of those days where I can’t see the forest for the trees, but I can’t figure out how to retrieve the dictionaries that are within the arrays.

Here’s the relevant code:

Dim v as Variant
v=ParseJSON_MTC(content, False, True) 'Content incoming from the URLConnection

'Returns a dictionary with four entries:
'The first is a dictionary
'The second and third are arrays of 4 objects (ubound=3), all of which are dictionaries
'The fourth is a dictionary

If v IsA Dictionary Then
  
  For Each ky As DictionaryEntry In Dictionary(v)
    
    If ky.Key="latest-build" Then
      
      'A single dictionary, easy
      
    Elseif ky.key="messages" Then
      
      If ky.Value.IsArray Then 'This is an array of objects, all of which are Dictionaries
        
        Dim dvalues As Variant
        
        For j=0 To UBound(Array(ky.Value))
          
          dVal=Dictionary(Array(ky.Value(j))) 'Not an array, but treating it as one" <<<<==== THE PROBLEM LINE
          
          If dVal IsA Dictionary Then
            For Each de As DictionaryEntry In Dictionary(dVal)
              KeyStr=de.Key
              Astr=de.Value
            Next
          End If
          
        Next
        
      End If

etc...

It must be something simple and obvious, thoughts?

Thank you -

  • John

Some kind of bug, i’ve reported lately, try:

dVal=Dictionary(Object(Array(ky.Value(j))))

Xojo does return Object() instead of Dictionary() in Variant from ParseJSON

<https://xojo.com/issue/62293>

Alas, no luck. Tried Dictionary(Object(Array, Array(Object(Dictionary and every other combination I can think of.

 Var objs() as object = dvalues
 For each obj as object in objs
 Var d as Dictionary = dictionary(obj)
 // do something with the doctionary.
 // catch exceptions too!
 Next

You can check the examples in the feeback report

Very clever, great thinking outside the box. Once again, the people on this forum are the BEST, thank you!

1 Like

FYI, when parsing JSON, you’ll only get one of two structures: Dictionary or Variant(), so this should work:

if ky.Value.IsArray then
  dim arr() as Variant = ky.Value
  for each d as Dictionary in arr
  ...

@Kem_Tekinay Not when receiving… again see the feedback case.
<https://xojo.com/issue/62293>

Internally ParseJSON uses Object() instead of Dictionary() so you must convert.
GenerateJSON does use Dictionary since you can’t parse objects() into it or it will trow an exception.

I know what the debugger says, but it’s an array of Variant. Try the code I posted to see.

Or try this code:

var s as string = "{""main"":[{""a"":1},{""b"":2}, true, null]}"
var d as Dictionary = ParseJSON( s )


var arr() as variant = d.Value( "main" )
for each child as variant in arr
  if child.Type = Variant.TypeObject then
    MessageBox Introspection.GetType( child ).Name
  else
    MessageBox child.Type.ToString
  end if
next