Hey everyone! I'm trying to extract a specific value from a large JavaScript file that's being served from a CDN. The file isn't in JSON format—it's structured as a JavaScript object. Here's a simplified example of what it looks like:
```javascript
var Ii = {
'strict': [
{ 'name': 'randoje', 'domain': 'example.com', 'value': 'abc%3dXYZ...' },
...
],
...
};
```
Right now, the only method I can think of is using a regex to grab the value 'abc%3dXYZ...'. But regex is a bit tricky for me, and I can't help but feel there might be an easier approach to extract this value. Any tips would be greatly appreciated!
6 Answers
If it were me, I'd probably just use JavaScript to get it. But if you need to do some calculations in Python, you could have a JS script write that variable to a JSON file instead. That could keep everything neat!
You could try using Python's `ast.literal_eval` after stripping 'var Ii =' from the start and the ';' from the end. Just a heads up, though, this can break easily if the JS structure changes and might lead to issues with untrusted input.
Is this a genuine question? I mean, with tools like ChatGPT available, it seems odd to ask this here.
Yeah, I've tried using ChatGPT, but it keeps suggesting regex, which is confusing!
This seems like an XY problem. What's your end goal by pulling that value out of the JS file? Maybe there's a more straightforward way to tackle your main issue instead of diving into this extraction.
You could use regex to extract the object, then treat it as JSON and grab the 'value' key. Just be careful because if the keys aren't double-quoted, you’ll hit a parsing wall.
Good point! I didn't realize that could be a problem. I'll have to check the formatting of the keys!
Not sure why you're going down this route, but here's a thought: maybe consider converting the 'strict' part into JSON and then parsing it. That might simplify things!

I think I'll try that, thanks for the suggestion!