Dictionaries

wbreidi's icon

Hello, I'm a begginer in the dictionary and Javascript worlds. I have a patch that can download a json file from a web site into dictionary with a [maxurl]. So far all goes well and I can view in my [dict.view] object the correct structure of my imported json file (see attached image). The problem is that I can't query the values of noise (the average key for example). If I send a "get status" message I get the value of status alright, but when I send dictionary a "get average" or "get noise::average" I get an error message saying "index out of range for key noise" ! I can't seem to find the right syntax for dictionary. Can anyone tell me how can I get access to the noise values, please !

Martin Beck's icon

Can you attach the dict content as txt file to your post?
Looks like it is a nested dict and that's why your query doesn't work.

wbreidi's icon

datastore_search_sql.txt
text/plain 0.48 KB

Hi Martin, here is a txt file of the dict content and another in .json format just in case. What is a nested dict?

{"help": "https://portal.opendata.dk/api/3/action/help_show?name=datastore_search_sql", "success": true, "result": {"records": [{"noise": {"max": 79, "average": 66.5, "min": 55.3}, "PM2.5": 0, "PM10": 0}], "fields": [{"type": "json", "id": "noise"}, {"type": "int4", "id": "PM10"}, {"type": "int4", "id": "PM2.5"}], "sql": "SELECT noise, \"PM10\", \"PM2.5\" from \"7e85ea85-3bde-4dbf-944b-0360c6c47e3b\" WHERE deviceid = '2c002e000f47373334363431' ORDER BY published_at DESC limit 1"}

dictjsonfile.json
application/json 1.41 KB


Martin Beck's icon

Well, I think patchers are not really a good solution for query of nested dicts.
I think it's better to go for a javascript solution for this (see e.g. helpfile of [dict]).
The attached patch gives the value you want, but the approach is not sufficiently generic.

Max Patch
Copy patch and select New From Clipboard in Max.


Nested dict means dict inside a dict. I don't know if this is the common term, but in other programming languages there is a similar concept or data type called nested lists or struct, which means that a certain structure can be embedded in child nodes that look similar or the same as the parent node structure.

I think there are better solutions to your problem than my patcher. For your understanding it might help to note the following. Every time a query gets a dictionary as result like in your screenshot dictionary u926001544 use a print object to see how many dicts were output. The names of these dicts are automatically created during runtime. You could build a patcher that takes all of these dicts and performs queries inside of each of them. But I think javascript is more convenient.

wbreidi's icon

Thanks a lot, it works fine for me. I don't have time to learn Javascript for the installation I am doing actually. I think I will use json files that have only one child node for every nested dictionary. Just one question though, the :: sign is the syntaxe indicating the path of the nested dictionary right?
Cheers

Martin Beck's icon

Hello WBREIDI,

finally found the syntax for directly accessing these nested or array like structures.

Max Patch
Copy patch and select New From Clipboard in Max.

wbreidi's icon

Hello Martin, I can't acces the patch !

Martin Beck's icon

Did you scroll down a bit? Objects keep their position in patch even if you only copy a part from a patcher.

wbreidi's icon

When I click on copy patcher I get a blank page with the title Dictionnaries, a few thumbnails and that's all ! I can't even send a reply from that page. It is very strange, I wonder why?

mizu's icon

Happy New Year all !
same here with Firefox and Thunderbird from yesterday... Opening the page in Safari works
michel

Martin Beck's icon

???

Max Patch
Copy patch and select New From Clipboard in Max.

Martin Beck's icon

...back to my computer. I don't know why the compressed patch is not accessible. At my computer it works with Safari and also with Chrome on Android phone.
Just in case it helps I attached the patcher (slightly modified version) as maxpat file.

queryNestedDict.maxpat
Max Patch

wbreidi's icon

Hello Martin,
I used Safari instead of Firefox and it works. The patch works too. I can go as far as two nested dictionaries but not 3 (index out of range for "field" 3), I can do with just two nested dictionaries, that's fine. Thanks a lot.