How to Convert Python Dict to JSON?
The key-value pair structure of a Python dict is very similar to that of JSON, but there are differences in syntax and data types. If you need to visualize or process a python dict, you can first convert it to JSON and then use the powerful JSON editing and visualization features on JSON For You. The steps are as follows:
- Paste the original JSON string into the left editor;
- Click "Search Command" at the top, find "Python dict to JSON", and click to execute.
Since there are significant differences between python dict and JSON, JSON For You currently only supports simple text replacement, so converting complex dicts may fail.
Feature | Python Dictionary | JSON |
---|---|---|
String Quotes | Single or double quotes | Double quotes only |
Trailing Commas | Allowed | Not allowed |
Comments | Allowed in code (not in dictionary literals) | Not supported |
Key Type | Can be any hashable type | String |
Value Type | Supports tuples, sets, datetime, and custom objects | Only supports strings, numbers, booleans, null, arrays, and objects |
Example
Python dict:
{
'name': 'John Doe',
'age': 30,
'is_student': False,
'hobbies': ['reading', 'coding', 'hiking'],
'address': {
'street': '123 Main St',
'city': 'Anytown'
},
}
Converted JSON:
{
"name": "John Doe",
"age": 30,
"is_student": false,
"hobbies": ["reading", "coding", "hiking"],
"address": {
"street": "123 Main St",
"city": "Anytown"
}
}