Receiving data via API in MaxMSP

Laura Papke's icon

Hi quick question,

I am currently trying to extract the values from a website but somehow I don't manage. Could someone give me a hint what I am doing wrong?

Here is the info I should receive from the API (I actually just need the values):

[
  {
    "id": "idbfd7c0ff-b8a7-40ff-a806-8f1d627c31d5",
    "deviceSerial": "DEMO/123",
    "channelIdentifier": "ch2",
    "name": "Single-ended 2",
    "interval": 1,
    "value": -0.2796277403831482,
    "multiplier": 1
  },
  {
    "id": "id41f511bd-cc81-4543-ab83-7449bf0925af",
    "deviceSerial": "DEMO/123",
    "channelIdentifier": "ch4",
    "name": "Single-ended 4",
    "interval": 1,
    "value": 1.107696294784546,
    "multiplier": 1
  },
  {
    "id": "idfb989352-6335-4686-a315-ddcbd4ef0628",
    "deviceSerial": "DEMO/123",
    "channelIdentifier": "ch6",
    "name": "Single-ended 6",
    "interval": 1,
    "value": 0.48941902816295624,
    "multiplier": 1
  }
]

TFL's icon

The problem here is that you need to ask maxurl to parse the result as JSON instead of plain text. For this you need to create a dict with an 'url' key containing your API url, and a 'parse_type' key set to 'JSON'. You can find an example of this is you search for tour_maxurl.maxpat in the Max search.

Then you need to understand the data you get from the API in order to properly treat it. In your case, the content of your body starts by a [ and ends by a ] so this is an array. That array contains three dictionaries (starting with { and ending with }, each separated by a coma ,) and these dictionaries contain the actual data you want to access.

Here is how to access your data:

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

Please, next time can you share your patch (select all relevant objects, then Edit -> Copy compressed, then paste here in your answer), so we don't have to copy very long URL with non-sense letters and numbers by hand!

Laura Papke's icon

Hey TFL,

Thank you so much for your answer! And sorry for coming back to this just now.

Everything works out perfectly, but I haven't really understood how I can extract the three values in separate boxes. I am able to print all of them at the same time, but I somehow don't manage to extract them from all three dictionaries individually. Do you have a hint for me?

Thank you :).

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

Source Audio's icon

you can get that 3 values without array object,

makes it compatible with older max version.

also it does not really have to parse json type

unless you need json for other things than get that 3 values.

to collect and distribute 3 values, try cycle object

Laura Papke's icon

Hey Source Audio,

Thank you for your help. I've just rebuild your patch and somehow I don't receive any value with cycle. Did I forget something?

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

Source Audio's icon

yes, a little detail, but it counts

Laura Papke's icon

Ah amazing thank you so much ;)