Skip to content
Advertisement

How to parse JavaScript Json into Python dict type, effeciently

I am looking for way to read javascript json data loaded into one of a script tag of this page. I have tried various re patterns posted on google and stackoveflow but got nothing.

The Json Formatter shows an Invalid (RFC 8259).

Here is a code

JavaScript

The problem seems an invalid json format. The type of profile_json is string while a little amendments in above code shows below error stack

JavaScript

Error in output are highlighted here:

json

The original HTML contains this (heavily trimmed):

JavaScript

The same string extracted by scrapy is this:

JavaScript

It appears the backslashes are removed from it, making the JSON invalid.

Advertisement

Answer

I don’t know if this is an efficient way of handling the problem but below code resolved my problem.

JavaScript
Advertisement