How to Convert Dictionary to JSON in Python

In this article, you will learn how to convert a dictionary to JSON in Python. Let’s say you have a dictionary named ‘a’. Convert Dictionary…

The post How to Convert Dictionary to JSON in Python appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli

In this article, you will learn how to convert a dictionary to JSON in Python.

Let’s say you have a dictionary named ‘a’.

# Import JSON module
import json   
       
# A dictionary named 'a'  
a = {   
  "Name": "Meat Pizza",   
  "Price": "$12",  
  "Size": "Regular",
  "Crust Type": "Deep Dish"
}  

Convert Dictionary to JSON in Python

In order to convert a dictionary to JSON, you can use the json.dumps() method.

# Import JSON module
import json   
       
# A dictionary named 'a'  
a = {   
  "Name": "Meat Pizza",   
  "Price": "$12",  
  "Size": "Regular",
  "Crust Type": "Deep Dish"
}   
       
# Convert dictionary named 'a' to JSON  
print(json.dumps(a, indent = 4)) 
# {
#     "Name": "Meat Pizza",
#    "Price": "$12",
#    "Size": "Regular",
#    "Crust Type": "Deep Dish"
# }

Note: The json.dumps() method functions by converting Python objects to JSON string.

The post How to Convert Dictionary to JSON in Python appeared first on CodeSource.io.


This content originally appeared on CodeSource.io and was authored by Ariessa Norramli


Print Share Comment Cite Upload Translate Updates
APA

Ariessa Norramli | Sciencx (2021-02-26T10:37:12+00:00) How to Convert Dictionary to JSON in Python. Retrieved from https://www.scien.cx/2021/02/26/how-to-convert-dictionary-to-json-in-python/

MLA
" » How to Convert Dictionary to JSON in Python." Ariessa Norramli | Sciencx - Friday February 26, 2021, https://www.scien.cx/2021/02/26/how-to-convert-dictionary-to-json-in-python/
HARVARD
Ariessa Norramli | Sciencx Friday February 26, 2021 » How to Convert Dictionary to JSON in Python., viewed ,<https://www.scien.cx/2021/02/26/how-to-convert-dictionary-to-json-in-python/>
VANCOUVER
Ariessa Norramli | Sciencx - » How to Convert Dictionary to JSON in Python. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/02/26/how-to-convert-dictionary-to-json-in-python/
CHICAGO
" » How to Convert Dictionary to JSON in Python." Ariessa Norramli | Sciencx - Accessed . https://www.scien.cx/2021/02/26/how-to-convert-dictionary-to-json-in-python/
IEEE
" » How to Convert Dictionary to JSON in Python." Ariessa Norramli | Sciencx [Online]. Available: https://www.scien.cx/2021/02/26/how-to-convert-dictionary-to-json-in-python/. [Accessed: ]
rf:citation
» How to Convert Dictionary to JSON in Python | Ariessa Norramli | Sciencx | https://www.scien.cx/2021/02/26/how-to-convert-dictionary-to-json-in-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.