JSON is a cross language, widely used method to serialize data
Supported data types : int, float, boolean, string, list and dict. See -> JSON Wiki(opens new window) for more
Here is an example demonstrating the basic usage of JSON :-
import json
families =(['John'],['Mark','David',{'name':'Avraham'}])# Dumping it into string
json_families = json.dumps(families)# [["John"], ["Mark", "David", {"name": "Avraham"}]]# Dumping it to filewithopen('families.json','w')as json_file:
json.dump(families, json_file)# Loading it from string
json_families = json.loads(json_families)# Loading it from filewithopen('families.json','r')as json_file:
json_families = json.load(json_file)
Using pickle or cPickle, it is the method that objects are being Serialized/Unserialized. You probably want to use pickle.HIGHEST_PROTOCOL here, which means the newest method.
Remarks
Why using JSON?
Cross language support
Human readable
Unlike pickle, it doesn't have the danger of running arbitrary code
Why not using JSON?
Doesn't support Pythonic data types
Keys in dictionaries must not be other than string data types.
Why Pickle?
Great way for serializing Pythonic (tuples, functions, classes)