
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
How do I serialize a Python dictionary into a string, and then back to a dictionary?
The JSON module is a very reliable library to serialize a Python dictionary into a string, and then back to a dictionary. The dumps function converts the dict to a string.
example
import json my_dict = { 'foo': 42, 'bar': { 'baz': "Hello", 'poo': 124.2 } } my_json = json.dumps(my_dict) print(my_json)
Output
This will give the output −
'{"foo": 42, "bar": {"baz": "Hello", "poo": 124.2}}'
The loads function converts the string back to a dict.
example
import json my_str = '{"foo": 42, "bar": {"baz": "Hello", "poo": 124.2}}' my_dict = json.loads(my_str) print(my_dict['bar']['baz'])
Output
This will give the output −
Hello
- Related Articles
- How do I format a string using a dictionary in Python 3?
- How to serialize Python dictionary to XML?
- How I can convert a Python Tuple into Dictionary?
- How to convert a String representation of a Dictionary to a dictionary in Python?
- How do I assign a dictionary value to a variable in Python?
- How to convert the string representation of a dictionary to a dictionary in python?
- How to define a Python dictionary within dictionary?
- Python - Convert a set into dictionary
- How to convert a string to dictionary in Python?
- How to translate a python string according a given dictionary?
- Convert string dictionary to dictionary in Python
- How to map two lists into a dictionary in Python?
- Convert dictionary object into string in Python
- Python program to create a dictionary from a string
- How can I convert a Python Named tuple to a dictionary?

Advertisements