json parsing

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)

4 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

Are there any examples of this working the other direction? Outputting TableDAT to JSON? I’m having trouble finding instances.

@michael_e see example here: How to convert Dat table to Json Object and send it out through OSC - #2 by nettoyeur

getting closer… but I think I need the “for dummies” version… ?
DAT_toJSON_3.5.toe (3.9 KB)
maybe @a547 can help unravel this one?

Hi @michael_e,

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:

{
    "chan1" : [-0.032423, -0.21334, -0.123123, ...... 0.138123]
}

Your CHOPExecute DAT then could look like this:

# 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

Hope this helps
Cheers
Markus

Brilliant - thank you, this is an enormous help!
Direct from CHOP = : )
Happy New Year
Michael