Dendi Handian
Posted on January 24, 2024
Exporter Function
import json
def export_json(filename, obj):
with open(filename, 'w') as writer:
writer.write(json.dumps(obj, indent=2))
Importer Function
import json
def import_json(filename):
obj = {}
with open(filename, 'r') as reader:
obj = reader.read()
return json.loads(obj)
Export and Import Dictionary (JSON object)
sample_dict = {
"maps": [
{'a': 1},
{'b': 2},
{'c': 3},
{'d': 4},
]
}
export_json('maps.json', sample_dict)
obj = import_json('maps.json')
print(type(obj))
print(obj)
Export and Import List (List of JSON Objects)
sample_list = [
{'a': 1},
{'b': 2},
{'c': 3},
{'d': 4},
]
export_json('maps.json', sample_list)
obj = import_json('maps.json')
print(obj)
print(type(obj))
💖 💪 🙅 🚩
Dendi Handian
Posted on January 24, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.