Use json.dumps() to convert a dictionary to a JSON-formatted string:
import jsonperson ={"first_name":"Ophelia","last_name":"Clarke","message":"Hi, thanks for the ice cream!","fav_flavors": ["Vanilla Bean","Mocha","Strawberry"]}raw_data = json.dumps(person)print(type(raw_data))#> <class 'str'>print(raw_data)#> '{"first_name": "Ophelia", "last_name": "Clarke", "message": "Hi, thanks for the ice cream!", "fav_flavors": ["Vanilla Bean", "Mocha", "Strawberry"]}'
Reading JSON
Use json.loads() to convert a JSON-formatted string into a Python dictionary.
raw_data ='{"first_name": "Ophelia", "last_name": "Clarke", "message": "Hi, thanks for the ice cream!", "fav_flavors": ["Vanilla Bean", "Mocha", "Strawberry"]}'person = json.loads(raw_data)print(type(person))#> <class 'dict'>print(person)#> {'first_name': 'Ophelia', 'last_name': 'Clarke', 'message': 'Hi, thanks for the ice cream!', 'fav_flavors': ['Vanilla Bean', 'Mocha', 'Strawberry']}
Reading and Writing JSON Files
Combine these JSON-parsing techniques with file management techniques to read and write JSON data from a .json file.
Writing:
json_filepath ="path/to/gradebook.json"# for example (see os module notes for constructing filepaths more reliably)withopen(json_filepath, "w")as json_file: json.dump(data, json_file)
Reading:
json_filepath ="path/to/gradebook.json"# for example (see os module notes for constructing filepaths more reliably)withopen(json_filepath, "r")as json_file: my_data = json.load(json_file)print(my_data)