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.
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.
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.
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)
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.
the datToJSON expects a json formated string in the DAT itself:
Turn JSON text stored in a DAT into a JSON Python object.
For what you want to do, you would have to define the format yourself. In your example you have a column header chan1 and then a bunch of rows with values. This could be interpreted as a key per column type json structure like:
# me - this DAT
#
# channel - the Channel object which has changed
# sampleIndex - the index of the changed sample
# val - the numeric value of the changed sample
# prev - the previous sample value
#
# Make sure the corresponding toggle is enabled in the CHOP Execute DAT.
import json
null1 = op('null1')
json_out = op('json_out')
def onOffToOn(channel, sampleIndex, val, prev):
myDict = {}
# loop through all columns
for c in null1.cols():
# first row in a column holds the key
key = c[0].val
# create a list for this key
myDict[key] = []
# loop through remaining rows and append values to dictionary
for item in c[1:]:
myDict[key].append(item.val)
# convert dictionary to json
jsonText = json.dumps(myDict, sort_keys=True, indent=4)
# output json to text dat
json_out.text = jsonText
return
As DATs in general hold strings, your output will have the values as strings as well. You could directly fetch from the CHOP as well, creating a list of float values:
# me - this DAT
#
# channel - the Channel object which has changed
# sampleIndex - the index of the changed sample
# val - the numeric value of the changed sample
# prev - the previous sample value
#
# Make sure the corresponding toggle is enabled in the CHOP Execute DAT.
import json
noise = op('noise1')
json_out = op('json_out')
def onOffToOn(channel, sampleIndex, val, prev):
myDict = {}
# loop through all channels
for c in noise.chans():
# set the key to the channel's name
key = c.name
# append all channel values to the key
myDict[key] = c.vals
# convert dictionary to json
jsonText = json.dumps(myDict, sort_keys=True, indent=4)
# output json to text dat
json_out.text = jsonText
return