import re
import json


def get_json_from_script_variable(soup, variable_string):
    """
    Helper for extracting json variable from script tag in html
    :param response: requests.Response
    :param variable_string: variable to extract
    :return: variable json
    """
    script = soup.find('script', text=re.compile('^\s*{}\s*=\s*\[.*?\]\s*;'.format(variable_string)))
    if not script:
        return None
    json_text = re.search('^\s*{}\s*=\s*\[.*?\]\s*;'.format(variable_string),
                          script.string, flags=re.DOTALL | re.MULTILINE)
    if json_text:
        return json.loads(json_text.group(0).split('= ')[1][: -1])