To use simplejson first easy_install simplejson:
import simplejson
my_structure = {"name":"Joe", "age":27, "numbers":[1,2,3,4,5], "subdict":{"first":1, "second":2, "third": 3}}
json = simplejson.dumps(my_structure)
results in json being:
{"age": 27, "subdict": {"second": 2, "third": 3, "first": 1}, "name": "Joe", "numbers": [1, 2, 3, 4, 5]}
Notice that its hardly changed the format of the dictionary at all, but you should run it through this step to ensure valid JSON data.
You can further pretty print the result:
import pprint
pprint.pprint(my_structure)
results in:
{'age': 27,
'name': 'Joe',
'numbers': [1, 2, 3, 4, 5],
'subdict': {'first': 1, 'second': 2, 'third': 3}}
repr(), and load them back in with ast.literal_eval. Eg a quick test you can do isimport ast; ast.literal_eval(repr(myvariable)), for saymyvariable=(1,2,3). If your lists/dictionaries/tuples contain non-built-in types, the logical thing to do is just save them in a readable .py module that defines the type and loads values, as you also need the type implementation. - Riaz Rizvi