From Chen's Wiki

Jump to: navigation, search

Question

How to quickly import Json into a Python program?

Solution

The json module in Python has

json.load(fp[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])

Of course, the first one should be a file handler, the others are bit of tricky. I will talk about cls and object_hook. You need to put a class constructor in cls and for object_hook, you can define your own method to deal with each record.

#For example
	data = json.load(json_data, object_hook= \
				lambda x: x[KEY].strip('[] ') if x[KEY]!=EMPTY else None)