
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to convert Javascript dictionary to Python dictionary?
Python and javascript both have different representations for a dictionary. So you need an intermediate representation in order to pass data between them. The most commonly used intermediate representation is JSON, which is a simple lightweight data-interchange format.
Example
The dumps function converts the dict to a string. For 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}}'
Example
The load's function converts the string back to a dict. For 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
On the JS side of things, you don't need to do anything. This is because JSON literally means JavaScript Object Notation. And JS implicitly constructs objects from JSON. If you get a string, you can convert it using JSON.parse().
- Related Articles
- Convert string dictionary to dictionary in Python
- How can I convert Python dictionary to JavaScript hash table?
- How to convert Python Dictionary to a list?
- How to convert a spreadsheet to Python dictionary?
- How to convert list to dictionary in Python?
- Python Convert nested dictionary into flattened dictionary?
- Python - Convert flattened dictionary into nested dictionary
- How to convert the string representation of a dictionary to a dictionary in python?
- How to convert a String representation of a Dictionary to a dictionary in Python?
- How to convert a string to dictionary in Python?
- How to convert Python dictionary keys/values to lowercase?
- How to convert dictionary into list of JavaScript objects?
- How to define a Python dictionary within dictionary?
- Convert dictionary to list of tuples in Python
- Convert tuple to adjacent pair dictionary in Python

Advertisements