Python – Dict of tuples to JSON
Last Updated :
28 Nov, 2021
In this article, we will discuss how to convert a dictionary of tuples to JSON.
This will convert dictionary of tuples to json
Syntax: json.dumps(dictionary, indent)
Parameters:
- dictionary is the input dictionary.
- indent specify the number of units of indentation
Example: Python dict of tuples to json conversion
Python3
import json
data = {
"id" : ( "1" , "2" , "3" ),
"name" : ( "bhanu" , "sivanagulu" ),
"department" : ( "HR" , "IT" )
}
final = json.dumps(data, indent = 2 )
print (final)
|
Output:
{
"id": [
"1",
"2",
"3"
],
"name": [
"bhanu",
"sivanagulu"
],
"department": [
"HR",
"IT"
]
}
This will write converted json data into file, which will be downloaded and saved on your computer.
Syntax: json.dump(dictionary,pointer)
Parameters:
- dictionary is the input dictionary.
- pointer is the file pointer that is opened in write or append mode.
Syntax:
with open("mydata.json", "w") as final:
json.dump(data, final)
where, mydata is the new JSON file
Finally , we have to download the created JSON file
Syntax:
files.download('mydata.json')
Example: Python dict of tuples to json conversion
Python3
from google.colab import files
import json
data = {
"id" : ( "1" , "2" , "3" ),
"name" : ( "bhanu" , "sivanagulu" ),
"department" : ( "HR" , "IT" )
}
with open ( "mydata.json" , "w" ) as final:
json.dump(data, final)
files.download( 'mydata.json' )
|
Output:
Share your thoughts in the comments
Please Login to comment...