Json parsing to table

Hi, I’m sure this is extremely easy but I’m having issues parse this json data into a table from a webclient DAT - https://api.carbonintensity.org.uk/generation

I’d like to parse each header into a column in a table DAT?

And how would I having a cache of the last 10 results? updating every half an hour/when the data changes?

For the first question:

Edit the webclient_callbacks on onResponse(webClientDAT, statusCode, headerDict, data):
The header might contain different things you are interested in like content-type, content-length, content-disposition, etc. (See MDN Web Docs HTTP Headers) you might want to account for.

To append them to your table you can use the methods to modify table content:
op('my_table1').appendCol(statusCode['code'], headerDict['content-type'], headerDict['content-lenght])
This would append a column to your table ‘my_table1’ with 3 rows, containing the status code the WebClient received and the content-type as well as the content-length.

Your JSON will come as the data of the request. You could write it to a TextDAT and filter it with the JsonDAT. If you also want it in the (or a) table you can append the data too.
I personally find it easier to import json and add the object I get from json.loads(data) into a table or whatever I need to work with. Most of the time saves the JsonDat.

To limit your table to 10 columns you might want to use a DAT Execute Node on it which triggers onSizeChange() and deletes the first column if the table got more than 10.

For the auto-update you could use a TimerCHOP which triggers the webclients request-button: op('my_webclient1').par.request.pulse() when its done, and auto-resets itself to start again.
Ther is a great tutorial on timers by Matthew Ragan on timers: THP 494 & 598 | The Timer CHOP | TouchDesigner - YouTube

HI all, If you put that JSON string into a Text DAT and as Drake_Rubicon says, pass that to a JSON DAT, you can see it as clean JSON.

You can refine the JSON down to the part that you want, like putting $…generationmix into the JSON Path parameter.

At any time, like in a DAT Execute DAT, you can refer to op(‘json1’).results as a python dictionary of the same data.

But there is a much simpler way to get a table:

The JSON DAT does not yet have a way to go to a table directly (we intend to) but there is a component that does this automatically. Use OP Snippets for the JSON DAT, example7 titled “make a table from json”, I’d start with jsonTabler2 and simply put in the JSONPath Filter1 parameter: $…generationmix[*]

That looks for a key called generationmix and the sub-json is converted to a table.

2 Likes