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,040 points
26,498 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
15,297 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
216 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
385 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,906 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,790 points
2,136 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
385 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
861 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP