I have a JSON file like this
$ cat a.json
{
"a" : 1,
"b" : [ 2 , 3 ],
"c" : {
"x" : 1,
"y" : [ 2 , 3 ]
}
}
I am trying to load and dump the data, however the dump part is not working.
I checked that my code is able to load the file but for some strange reason not able to print it to the terminal using json.dumps()
My code:
$ cat jlo.py
import json
import pprint
class JLO():
def __init__(self):
try:
with open("a.json",'r') as inFile:
config = json.load(inFile)
except Exception as e:
print "Can't read JSON config" + str(e)
exit(1)
self.config = config
print "Main"
print "================"
jlo = JLO();
json.dumps(jlo.config, sort_keys=True, indent=4)
print "================"
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(jlo.config)
print "================"
Output:
$ python jlo.py
Main
================
================
{ u'a': 1, u'b': [2, 3], u'c': { u'x': 1, u'y': [2, 3]}}
================
I am on Python 2.6
json.dumps
just creates the string, you never actuallyprint
it... – jonrsharpe Oct 26 '15 at 12:15import
json
, soimport pprint
is unnecessary. The Pretty Print formatting is an option ofjson.dumps()
. – jesterjunk Oct 26 '15 at 13:44