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
• 2,980 points
164 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

answered Sep 4, 2019 by Neel
• 3,010 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 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
4,213 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 25, 2019 in Python by Mohammad
• 3,230 points
76 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
133 views
+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 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,081 views
0 votes
0 answers
+4 votes
6 answers
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,010 points
116 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,010 points
125 views