Nested Dictionaries

After parsing a JSON string using ParseJSON a dictionary is created.
This dictionary however has dictionaries inside.
How does one access the data from a dictionary inside of another dictionary?

Brian

One way is to assign the value to a Dictionary variable.

dim dictInner as Dictionary = dictOutter.Lookup("TheKey", nil)

I use nested dictionaries all the time, so I created these handy extensions to the dictionary class which makes them much easier to use.

[code]Public Function getSubDict(extends dict as dictionary, key as variant) as dictionary
// This assumes that you’re storing a dictionary within a dictionary and returns it.
// If the key isn’t found, it returns an empty dictionary.

return dict.lookup(key, new dictionary)
End Function

Public Function getSubitem(extends dict as dictionary, key as variant, subkey as variant) as variant
// This assumes that you’re storing a dictionary within a dictionary. This looks
// up the subkey in that secondary dictionary and returns it.
// If either key isn’t found, it returns nil.

dim subDict as dictionary = dict.getSubDict(key)
return subDict.lookup(subkey, nil)
End Function

Public Sub setSubDict(extends dict as dictionary, key as variant, value as dictionary)
dict.value(key) = value
End Sub

Public Sub setSubItem(extends dict as dictionary, key as variant, subkey as variant, value as variant)
dim subDict as dictionary = dict.getSubDict(key)
subDict.value(subkey) = value
dict.setSubDict(key, subDict)
End Sub
[/code]

or use casting to access a value

Dim i as integer = dictionary( mydict.value( "subDicName" ) ).value( "maIntager" )

First, thanks for the responses so far, let me try and clarify a bit.
First, the issue with nested dictionaries has come from paring a JSON file from a service using: Xojo.Data.ParseJSON.
Using ParseJSON requires the use of Xojo.Core.Dictionary.
ParseJSON converts objects in JSON into dictionaries.

I can access nested dictionaries which start at the root of the main dictionary (Thanks Marc Zeedar).
So let me make an example:

I have organization:
This creates a dictionary called myOrg
In the root of myOrg, you can have many key value pairs
Then in the root you can have dictionaries as an example: myOrg_Location(street_address, city, state,phone.)
Then you can have dictionaries that have dictionaries:
as an example: myOrg_Members
for each Member a dictionary is created which then carries their details in key value pairs

MyOrg (main dictionary)
idx: 0 key: myOrg_Name value : MyOrganization
idx: 1 key: myOrg_Location value :Xojo.Core.Dictionary (dictionary)
Idx: 0 key: Address value: 123 ABC Street
Idx:1 key: phone value: 18005551212

   idx: 2   key: myOrg_Members   value: Auto (number of entries)
             row: 0          value: Xojo.Core.Dictionary
                      Idx: 0           key: FirstName        value: John
                      idx: 1           key: LastName        value: Doe
              row: 1         value: Xojo.Core.Dictionary
                       idx: 0           key: FirstName        value: Mary
                       idx: 1           key: LastName        value: Smith

So, my question is how do access or copy the dictionary which is shown on a ROW number with no key or value?

Brian

dim i as Integer = Dictionary(mydict.Value("subDicName")).Value("maIntager")

Fixed that for you ??

You can iterate through the items in a Dictionary.
http://docs.xojo.com/index.php/Dictionary.Keys will get you all of the keys in the dictionary
Then just fetch all the values with http://docs.xojo.com/index.php/Dictionary.Value

Store the data in whatever way works best for the application. When you start getting complex like this you’ll find it much easier to use classes instead of a clusterquack of dictionaries. Parse the JSON to be usable by the class in an object oriented and easy way.

Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.