[SHARING] parsing dictionaries from [maxurl]
Friends
I would like to share with you the trifle that I recently wrote: it's a simple JavaScript mechanism for parsing dictionaries (especially from the [maxurl]). Useful if you need to process data obtained from sources communicating over various web-APIs (such as OpenWeather, Binance, Wagi, etc.).
Recently there was some questions about related issues on the forum and most of the problems are connected to the dictionaries. Dictionaries are somewhow hard to handle for many Max users (mainly for those who do not use scripts). From the other hand, writing JS code handling dictionaries is, nearly always, boring and tedious (at least for me).
So, my script is iterating over the dictionary created by the [maxurl], generates and broadcasts set of two-element lists (key name, value) from the dictionary. These lists can be easly handled in your patches using trivial objects, such as [route], etc.
The script gives all keys unique names (naming schame is based on the original dictionary structure, so it's easy to understand) to avoid eventual problems with the keys with overlapping names nested in various parts of the dictionary.
Btw. the script may serve also as an iterator for dictionaries created with different (than [maxurl]) methods - of course, in this case, it may require some sort of fixes, to adapt it to a specific context.
thanks!
Useful, thanks.
Thanks Yaniki !
Thank you so much for this!
Very nice, Yaniki!
But if you don't want to mess with JS, you can do the same just with just stock objects: pick "body", parse dict, pick your data.
@KLSDIZ
Sure, you can. The method depends on specific requirements and individual "style". I'm using scripting very intensely.
@yaniki No prob! I read in your OP something about writing the code being boring and tedious, so... ;-) Peace!
Haha... yes, this is very good... something like "let's do something boring again (and again)"
I meant programming the same functionality (doesn't matter by coding or patching). So my script is kind of an universal solution (just parsing any dict without the need for hardcoding into patch values specific for the particular case).
Of course, in most cases you have to hardcode particular references ("names" of values) in a different part of your patch anyway (which is probably a good example showing how coding and patching may be equivalent).
Interesting topic...
@yaniki Indeed interesting. So you parse the body of the server response and then send out any data as tagged values. But the problem arises when the data have different format (shape) than singular numbers or strings. JSON can have arbitrary structure, eg. an arrays of numbers mixed with embedded objects. This kind of data doesn't have a natural representation in Max message system. Hence I think there is no one-size-fits-all solution.
Btw. There is JSON parser built in JS implementation. Here is my attempt to recreate your script using existing Max capability. A few times shorter and a few times faster. Not tested that much, but seems to work correctly.
OK. Enough procrastinating for today, back to boring and tedious work ;-). Have a nice day!
O, that's a nice solution. Thanks for sharing.
great thread this one :)
Friends is it possible to send data of one variable from one computer to another using the object maxurl?
@Sonichel
As far as I understand maxurl objects don't directly communicate with each other - they make requests to servers.
Having that said, specially with max 8 and n4m, you should be able to create an overkill communication system with it ;)