Python: Store a given dictionary in a json file
39. Store Dictionary Data in a JSON File
Write a Python program to store dictionary data in a JSON file.
Sample Solution:
Python Code:
Sample Output:
Original dictionary: {'students': [{'firstName': 'Nikki', 'lastName': 'Roysden'}, {'firstName': 'Mervin', 'lastName': 'Friedland'}, {'firstName': 'Aron ', 'lastName': 'Wilkins'}], 'teachers': [{'firstName': 'Amberly', 'lastName': 'Calico'}, {'firstName': 'Regine', 'lastName': 'Agtarap'}]} <class 'dict'> Json file to dictionary: {'students': [{'firstName': 'Nikki', 'lastName': 'Roysden'}, {'firstName': 'Mervin', 'lastName': 'Friedland'}, {'firstName': 'Aron ', 'lastName': 'Wilkins'}], 'teachers': [{'firstName': 'Amberly', 'lastName': 'Calico'}, {'firstName': 'Regine', 'lastName': 'Agtarap'}]}
Flowchart:
For more Practice: Solve these Related Problems:
- Write a Python program to write a dictionary to a JSON file with proper formatting and indentation.
- Write a Python program to convert a dictionary to a JSON string and then save it to a file.
- Write a Python program to implement error handling while saving dictionary data into a JSON file.
- Write a Python program to read a dictionary from a JSON file and print its contents in a formatted manner.
Go to:
Previous: Write a Python program to match key values in two dictionaries.
Next: Write a Python program to create a dictionary of keys x, y, and z where each key has as value a list from 11-20, 21-30, and 31-40 respectively. Access the fifth value of each key from the dictionary.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.