Is it possible to save python dictionary into json files

0 votes
i have a dictionary in Python and I want to save it in json file. How to achieve this?
Sep 4, 2019 in Python by Arvind
• 3,020 points
10,773 views

2 answers to this question.

0 votes

Suppose you have a dictionary in python say my_dict = {'a':11,'b':22,'c':33}. Refer to the following code - 

import json
my_dict = {'a':11,'b':22,'c':33}
with open("your_json_file", "w") as fp:
    json.dump(a,fp) 

json.dumps is mainly used to display dictionaries as a json format with the type of string. While dump is used for saving to file

Hope this helps!!

If you need to learn more about Python, It's recommended to join Python Training today.

Thanks!

answered Sep 4, 2019 by Neel
• 3,020 points
0 votes

You can save the Python dictionary into JSON files using a built-in module json. We need to use json.dump() method to do this.

Example:

import json

# assume you have the following dictionary
sampleDict = { "name": "Emma", "rollNumber": 5 }
with open("student.json", "w") as write_file:
    json.dump(developer, write_file, indent=4)

Use the indent parameter to prettyPrint your JSON data. You can also use sort_keys to sort dictionary keys while writing it in the JSON file.

Reference: json.dumps()

answered Mar 17, 2020 by Vishal
• 180 points

Related Questions In Python

0 votes
1 answer

Is it possible in Python requests to print entire HTTP request?

A better idea is to use the ...READ MORE

answered Nov 26, 2018 in Python by Nymeria
• 3,540 points
12,034 views
0 votes
1 answer
0 votes
1 answer

is it possible to do method overloading in python?

You cannot have two methods with the ...READ MORE

answered Mar 26, 2019 in Python by Mohammad
• 3,230 points
168 views
0 votes
1 answer

Is it possible to create an array with all values as zero in python?

You can use  np.zeros(4,3) This will create a 4 ...READ MORE

answered May 24, 2019 in Python by Anjali
295 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,475 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,855 views
0 votes
1 answer

Is it possible to run a function in Python using the command line?

Suppose your file name is demo.py and ...READ MORE

answered Jun 26, 2019 in Python by Neel
• 3,020 points
299 views
+1 vote
1 answer

is it possible to compare a values to a backslash in Python?

The backslash "\" character is used to ...READ MORE

answered Jun 28, 2019 in Python by Neel
• 3,020 points
627 views