json parsing

Could anybody provide a simple example how to work with json data from web dat?

Is it possible to use thiis script somehow to convert json to xml? code.google.com/p/pyang/source/ … n/json2xml

You could use that, but then you’d need to figure out what to do with the XML.
How is the json structured, and what information do you want to extract from it?

I’ve made two examples, the first one does all in python and the second one uses the WEB DAT to retrieve the json from a url. They both retrieve from jsontest.com)

open the Text Port in Touchdesigner (Alt+t) and run both scripts.
json_python_example.toe (4.13 KB)

nettoyeur’s example is probably better than what i’ll note down quick, but for anyone who just needs to see it in Python really quick.

You need to first import json library:

import json

load the json object using:

stringVar = '{"key":"value"}'
jsonObj = json.loads(stringVar)

then you can access it like a dictionary, so if you wanted to get value from key you could do:

print(jsonObj['key'])

Any other more complex structures you’d approach just how you work with dictionaries in Python which there are a ton of tutorials online, but that’s about the jist of it.

Thank you gentlemen! Nettoyeur - its stiil too hard to me - please make these DATs… connected :slight_smile: In this scenario - it would be cool if it output a table with two columns - date and time. Sorry for being so noob

Do you have a sample json which you would like to parse? As the structure of a json string can have any shape or form, it is necessary to start with an actual json sample string.

cheers
Markus

this one pubapi.cryptsy.com/api.php?metho … rketid=300
or this: bittrex.com/api/v1.1/public/get … h&depth=50
(but it seems that web dat doesn’t work with https)

Attached is a example that would deal with your links (I had no problem with the https in the web dat… it’s not connecting for you?)

To parse through what the webdat returned I added a DatExecute DAT which runs the callback whenever the webDAT changes.

Now you need to know what you are looking for in the returned json. With that in mind I created 3 Table DATs:
one for general information
one for the sell orders and
one for the buy orders

The script first parses the json into a python dict object and then parses that dictionary to fill in the 3 tables.

As the structure for the 2 links you provided is different from each other, the second datexecute has a bit changed script.
I found one issue wth the json format and python here though. As the Rate and Quantity in your second link are saved as floats, the json library rounds them. I couldn’t find a way to actually retrieve the original number.

Hope this helps
cheers
Markus
jsonExample.3.toe (12.1 KB)

Snaut, thank you very much for being so great!

Hey guys,

Thanks for the example and the tips! Working all fine on my side.

My question is more general, shouldn’t we check the status code (e.g. 200, 404, …) to check the response before parsing the JSON? How is it possible to get access to this code in the Web DAT?

The only way I’ve found so far is by including the headers, and then having to do a manual parsing to extract the status code, and find the beginning of the json format.

Any smarter/better way?

Thanks!

Hey

i’ve run into this thread as i’m wanting to retrieve data from json, which has a number of children datasets inside “value” i’m wanted to display.

(i’ve been fine with examples as of above, but are running into issues with backslashes and children in my json)

This is the link below.

{“feed_id”:1336320,“value”:"{“pm25”: “1.50”, “pm10”: “6.20”, “co2”: “690.00”, “tvoc”: “44.00”, “tempC”: “24.27”, “tempF”: “75.69”, “humidityRH”: “31.30”, “pressurePa”: “101869.19”}

I can get the parent “value” to a table using.

import json

def onTableChange(dat):
response = dat.text

imported_data = json.loads(response)


op('forDispay1')[0,0] = "value"
op('forDisplay1')[0,1] = imported_data["value"]

return

Please could someone advise how i’d get say “tempC” data into the table, which is a child of “value”.

Thanks in advance.

imported_data[‘value’][‘temp’]

thanks
I’ve just tried still, getting an error thrown out.
please see below

seems like the value member of the imported data does not exist. Please post in image of the refferenced dat and maybe print out the imported_data dictionary so we can understand whats actually inside.

Here’s the children, of whats thrown out of the ‘value’

Wow! That topic when it’s all started as an idea to visualize exchange orderbook in 3d, and became a trading and analisys bot (totally TD-based) years later.

@snaut @nettoyeur @elburz can I buy you a box of beer for kickstarting me to study python? Plz DM your crypto wallet address (xmr preferable)

3 Likes

Haha that looks awesome. I’d probably use that. Been watching my eth go to the moon :laughing: :laughing:

No XMR needed, enjoy the new super powers and try to help some other new folks if you’ve learned some cool tricks they can use :slight_smile:

1 Like

Wow @a547 that looks massive! Happy to hear you found & rocked your passion. Thanks for your offer but I don’t need a reward (this screenshot is rewarding enough! ) - but as Elburz said, I’d like you to pay it forward.

By the way, if you still are parsing JSON often, some nifty add-ons for that in latest experimental release: https://docs.derivative.ca/Experimental:JSON_DAT

1 Like