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 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.
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ā]
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.
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)
Haha that looks awesome. Iād probably use that. Been watching my eth go to the moon
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
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
Are there any examples of this working the other direction? Outputting TableDAT to JSON? Iām having trouble finding instances.