Programming Tip -How can I convert Python dictionary to JavaScript Json hash table?

in #steemit3 years ago

Programming Tip -How can I convert Python dictionary to JavaScript Json hash table?

Javascripthon-python-js-converter.jpg


Today i am going to show Programming Tip - How can I convert Python dictionary to JavaScript hash table with simple code. I hope you will understand this.
If you have any query you can ask.
i hope you will like it.


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)

This will give the output:

'{"foo": 42, "bar": {"baz": "Hello", "poo": 124.2}}'


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'])


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. 


Follow me at : https://steemit.com/@ahmadhassan
=========================================================


Thanks for reading and always welcome your suggestions :) =========================================================