Handling JSON in JavaScript

Handling JSON in JavaScript is basicly the same as in TouchDesigner. You can create simple objects and convert them to a JSONString and vice versa.
Lets give it a try by creating a string variable with our json string. Note that I always use ” ” in the JSONString. This is integral, so defining a JSONString has to be in ‘ ‘.
You can acces the member of an object in two ways. One way is by using the same notation as in python using [ ] and a string as the key (which is perfect for programatic use) or via the .notation

var json_string = '{	"Display1":{"Resolution"	:	{ "x":1024, "y":1024},
				    "Refreshrate"	:	25},
			"Display2":{"Resolution"	:	{ "x":512, "y":512},
				    "Refreshrate"	:	25} 
					}';
var json_object = JSON.parse( json_string);
console.log( json_object.Display1.Resolution.x);
console.log( json_object['Display1']['Resolution']['x']);

This will print out 1024 and 512 into the console.

To create an object directly without parsing, just delete the ‘ ‘. The notation stays exactly the same. For converting the object to a string, we will use the stringify method of the json class.

var json_object = {	"Display1":{"Resolution"	:	{ "x":1024, "y":1024},
				    "Refreshrate"	:	25},
			"Display2":{"Resolution"	:	{ "x":512, "y":512},
				    "Refreshrate"	:	25} 
					};
var json_string = JSON.stringify( json_string);
console.log( json_string);