
- 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 can I convert Python dictionary to JavaScript hash table?
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.
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}}'
example
The load's function converts the string back to a dict.
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
- How to convert Javascript dictionary to Python dictionary?
- How I can convert a Python Tuple into Dictionary?
- How can I convert a Python Named tuple to a dictionary?
- How do Python dictionary hash lookups work?
- Convert string dictionary to dictionary in Python
- How to convert Python Dictionary to a list?
- How to convert a spreadsheet to Python dictionary?
- How to convert list to dictionary in Python?
- How can I convert a Python tuple to string?
- How can I convert bytes to a Python string?
- How can I convert Python tuple to C array?
- How can I convert a string to boolean in JavaScript?
- Add elements to a hash table using Javascript
- Hash Table Data Structure in Javascript
- Creating a hash table using Javascript

Advertisements