Skip to content

Instantly share code, notes, and snippets.

@DarthJahus
Created January 26, 2015 19:24
Show Gist options
  • Save DarthJahus/e2a7c04ed30b8100a0b0 to your computer and use it in GitHub Desktop.
Save DarthJahus/e2a7c04ed30b8100a0b0 to your computer and use it in GitHub Desktop.
import json
# loading from file
def load_json_file(file_name):
with open(file_name, 'r') as _file:
content = _file.read()
content_dict = json.loads(content)
return content_dict
# saving to a file
def save_json_file(dict_obj, file_name):
with open(file_name, 'w') as _file:
json_content = json.dumps(dict_obj)
_file.write(json_content)
# usage
my_dict = {"a": 1, "b": "foo", 3: "bar"}
my_file = "F:\my.json"
print("Saving %s into JSON file %s...\n" % (my_dict, my_file))
save_json_file(my_dict, my_file)
print("Loading JSON file %s...\n" % my_file)
new_dict = load_json_file(my_file)
print("Loaded: %s\n" % new_dict)
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment