Reading json file / openweathermap API with Max 5

timh's icon

Dear all,


For an installation I try to pick out values i get from a openweathermap API
(similar to this example:

http://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22 )

In max 6 or 7 its not a big deal – there is the [dict] object that makes it really easy (get::main etc.).
In the older max5 version [dict] isnt there. so i also tried to access the data [text] (message file format json) but i only get a string of all datas. Does anybody has an idea how to get the single values (like cloudiness, wind, etc.) in max version 5? the problem also is, that the API can change. e.g. some datas exists, next day maybe they are gone. so there will be not the possibility to slice the text counting the characters etc. it would be nice to use [route] or [OSC-route] to get the data from the API.
Do i have to convert the json into a list by e.g. processing or is there a stable solution within max5?

I am also wondering if I should work with mxi sadam.dom (sax?), using xml format instead of json, but maybe thats not the right way. for now it would be great using the nice structure of json file…

It would be great if someone can help me to solve the problem with some nice hints!

(in the installation i have to use max 5 because i am using a fixed system running on a PPC G5. Max6 is not supported on PPC…)

thanks a lot!!

best,
Tim

vichug's icon

i suppose you should code a parser as javasscript directly, especially if you want to stick with the js syntax

Robin Parmar's icon

I am sure you have considered this and it's outside your constraints but...

Mac hardware is a pain. Especially older stuff. A cheap PC is cheaper than the value of your time. I've used recycled laptops from a decade ago. Windows 10 is free. And then you can run Max 7. Job done.

timh's icon

Dear Vichug,
that is what I feared… but at the end maybe the only feasible solution.

+

Dear Robin,
surely I thought about that – but there is installed a nice working system, no crashes since a couple of years – really stable! – and i will not replace that by a windows system – sorry ;)
(i am not that afraid about PPC architecture ;)

okay, so there will be an external solution to filter the json file into a string / list, max5 is able to read.
Maybe there are some workarounds in max5 (or extern) you can recommend?

Many thanks!

best,
tim

MakePatchesNotWar's icon

Maybe try jit.uldl and extract the values with regexp. Im not sure if the proposed javascript idea is supposed to use XMLHttpRequest but thats not available in 5.

MakePatchesNotWar's icon

Unfortunately i kinda forgot how to use regexp but this should get you started...

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

timh's icon

Thank you very much for the idea using regexp!
I am going to figure out, if that is a feasible solution. for the first try it was not doing what i wanted to – it is just slicing between characters: the syntax of the json might be too complex maybe…
But anyway– i will give it a second try and let you know if I get it running! thanks!!

One last thing: I don't insist on using the json format.
openweathermap also supports HTML format and XML. Maybe there is another max-implemented solution we can include in our patch to make the data from the weather API usable in our installation context…?

Thanks a lot!

all the best,
Tim

Robin Parmar's icon

Max is terrible for text parsing. I suggest doing this in Python or something, and feeding Max a clean data stream, through simple text files.

Though Max 7 is much more capable, the same principle applies.

MakePatchesNotWar's icon

" I am going to figure out, if that is a feasible solution "

This is a feasible solution if you can get the regexp to work and it didn't work because, like i said, i forgot how and i could already feel my frustration levels rising again so i'd rather avoid that. I just added it so you'd have a clue on how it could potentially work and regardless of format (json,html,xml) you will need regexp to work .

Like Robin said text processing is somewhat of a pain with Max.

timh's icon

Dear all!
Thank you very much for thinking and your ideas!
at the end we solved the problem by a elegant and simple python script to translate the json API into an easy-to-read text-list of the numbers we need.

Thanks!!

all the best,
Tim

Robin Parmar's icon

Glad you took that route. Python is an analgesic... it prevents headaches.

Many times I wonder why Cycling didn't embed Python into Max and save millions of problems. No-one with experience in both deliberately chooses Javascript. It's nice to see Node.js integration coming in version 8. But I recall even some of the Node developers said they would prefer to be working with Python!

ygreq's icon

Hi Robin! How did you manage to get the results into Max? I was trying something similar, meaning I was trying to get the results in Max from this: https://api.binance.com/api/v1/ticker/price?symbol=LTCBTC

Thank you so much for your help!

Robin Parmar's icon

I have used the [text] object. Format a tab delimited file outside of Max. Read it from a known location into [text]. When you spit out the data you can [unjoin] each line and the tabs are respected.

ygreq's icon

Thank you, Robin! In the end I found a solution. If anyone is interested, please check the link above to that post.

All the best!
ygr