Skip to content
Advertisement

reading JavaScript object in a file into a Python array

I have a javascript file that has an object I’d like to be read by Python (Python 3 is just fine). Something like this:

JavaScript

I want to convert myObject to a python dict object. Note I don’t really need the functions, just keys and values.

I’m fine with (and capable) adding comment markers before/after and isolating the object. But I think I need a library to convert that string into a Python dict. Is this possible?

Advertisement

Answer

Doing this using python would be a lot of work that can be avoided if you could add a few things directly in your javascript file. (as you told you could modify the js file)

I assume that you have nodejs and npm preinstalled (if not then you can install it from here

You need to add these lines of code at the end of the JS file.

JavaScript

once you add this you can run the js file by

JavaScript

This will make a new file named myjsonfile.json with the data which you can load from python like this

JavaScript

😉

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement