Search code examples
pythonparsing

parse js string to python


I have a string with js code

 [{label: ' ', name: 'Confirmed', width: 30, formatter: 'checkbox'},{label: 'ID', name: 'ID', width: 100, key:true},{label: 'E-mail', name: 'F250275', width: null, formatter: clearText},{label: 'Agree', name: 'F250386', width: null, formatter: clearText},]

Is there a way to parse it to python to get list? I've tried json.loads , but get the error json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 3 column 18 (char 27)

Some update, based on comments.

There isn't any options to use JS here. That's why I didn't put JS tag here


Solution

  • Here is the package for such things https://pypi.org/project/calmjs.parse/

    Some example

    from calmjs.parse.unparsers.extractor import ast_to_dict
    from calmjs.parse import es5
    
    my_string_with_js = "here is js code from the question"
    configuration = es5(my_string_with_js)
    baseconf = ast_to_dict(configuration)