How to convert a raw javascript object to a python dictionary?

When screening a screen on a website, I retrieve the data from the <script> tags.
The data I receive does not comply with the JSON standard. I can not use json.loads() .

 # from js_obj = '{x:1, y:2, z:3}' # to py_obj = {'x':1, 'y':2, 'z':3} 

I am currently using regex to convert raw data to JSON .
But I feel bad when faced with a complex data structure.

You have some better solutions. Thanks for participating!

+6
source share
3 answers

demjson.decode()

 import demjson # from js_obj = '{x:1, y:2, z:3}' # to py_obj = demjson.decode(js_obj) 

jsonnet.evaluate_snippet()

 import json, _jsonnet # from js_obj = '{x:1, y:2, z:3}' # to py_obj = json.loads(_jsonnet.evaluate_snippet('snippet', js_obj)) 

ast.literal_eval()

 import ast # from js_obj = "{'x':1, 'y':2, 'z':3}" # to py_obj = ast.literal_eval(js_obj) 
+16
source

This will most likely not work everywhere, but in the beginning, here is a simple regular expression that needs to convert keys to quoted strings so you can go to json.loads. Or is that what you are already doing?

 In[70] : quote_keys_regex = r'([\{\s,])(\w+)(:)' In[71] : re.sub(quote_keys_regex, r'\1"\2"\3', js_obj) Out[71]: '{"x":1, "y":2, "z":3}' In[72] : js_obj_2 = '{x:1, y:2, z:{k:3,j:2}}' Int[73]: re.sub(quote_keys_regex, r'\1"\2"\3', js_obj_2) Out[73]: '{"x":1, "y":2, "z":{"k":3,"j":2}}' 
+1
source

Just:

 import json py_obj = json.loads(js_obj_stringified) 

The above is part of the Python code. In the javascript part of the code:

 js_obj_stringified = JSON.stringify(data); 

JSON.stringify turns the Javascript object into JSON text and stores that JSON text in a string. This is a safe way to pass (via POST / GET) a javascript object for python processing.

-2
source

Source: https://habr.com/ru/post/970289/


All Articles