Python json

From wikinotes
Revision as of 18:01, 25 November 2016 by Will (talk | contribs) (→‎write)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

TODO:

use and document the usageof simplejson

datatypes

JSON natively supports 6x datatypes:

// simple datatypes
{
	"string"  = "my string",
	"number"  = 9999,
	"boolean" = true,
	"null"    = null
}	

// compound datatypes
{
	"object" = { "a":1, "b":1 },  // a dictionary
	"array"  = [ "a","b","c" ]
}

write

my_dict = {
	'a': 1,
	'b': 2,
}

with open( filepath, 'w' ) as fw:
	fw.write( json.dumps(my_dict, indent=2) )

read

with open( filepath, 'r' ) as stream:
	my_dict = json.load(stream)