# # JSON generator and printer # import json # # open the file using with # with open("example.json") as jf: jfinp = jf.read() print "---------------------------" # now print out the contents print 'Here is the JSON in the file:' print jfinp print "---------------------------" x = raw_input("Press return to continue") # now save it as a dictionary print 'Now we decode it;', jfdict = json.loads(jfinp) print 'its type is', type(jfdict) print jfdict print "---------------------------" x = raw_input("Press return to continue") print 'Now we dump it;', jfdump = json.dumps(jfdict) print 'its type is', type(jfdump) print jfdump print "---------------------------" x = raw_input("Press return to continue") # let's see what's there print 'Key is glossary' print jfdict["glossary"] print 'Key is glossary:title' print jfdict["glossary"]["title"] print "---------------------------" x = raw_input("Press return to continue") print 'Now we prettyprint it' print json.dumps(jfdict, indent=4, separators=(",", ": ")) print "---------------------------" x = raw_input("Press return to continue") print 'Now we prettyprint it another way' print json.dumps(jfdict, indent=4, separators=(",", " -- "))