Skip to content

Instantly share code, notes, and snippets.

@gustavorv86
Created May 22, 2019 06:30
Show Gist options
  • Save gustavorv86/6b5c962184ad2181aa7f4c5fcf39f3c2 to your computer and use it in GitHub Desktop.
Save gustavorv86/6b5c962184ad2181aa7f4c5fcf39f3c2 to your computer and use it in GitHub Desktop.
Serialize and deserialize python dictionaries to JSON format.
import json
import sys
def serialize(json_data, json_filename):
try:
json_string = json.dumps(json_data, sort_keys=True, indent=4)
fd = open(json_filename, "w")
fd.write(json_string)
fd.close()
return True
except Exception as ex:
print("ERROR {}: {}".format(ex.__class__.__name__, ex), file=sys.stderr)
return False
def deserialize(json_filename):
try:
fd = open(json_filename, "r")
json_string = fd.read()
fd.close()
return json.loads(json_string)
except Exception as ex:
print("ERROR {}: {}".format(ex.__class__.__name__, ex), file=sys.stderr)
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment