How to convert JavaScript dictionary into Python syntax
- by Sputnix
Writing out javascript dictionary from inside of JavaScript- enabled application (such as Adobe) into external .jsx file (or any other .txt file) the context of resulted file dictionary looks like:
({one:"1", two:"2"})
(Please note that each dictionary keys are written as they are the variables name (which is not true).
A next step is to read this .jsx file with Python. I need to find a way to convert ({one:"1", two:"2"}) into Python dictionary syntax such as:
{'one':"1", 'two':"2"}
It has been already suggested that instead of using JavaScript's built-in dict.toSource() it would make more sense to use JSON which would write a dictionary content in similar to Python syntax. But unfortunately using JSON is not an option for me. I need to find a way to convert ({one:"1", two:"2"}) into {'one':"1", 'two':"2"} using Python alone. Any suggestions on how to achieve it? Once again, the problem mostly in dictionary keys syntax which inside of Python look like variable names instead of strings-like dictionary keys names:
one vs "one"