import re
import json


def ghe_json_message(rec):
    """Given a GHE log, extract the JSON payload from the message field

    Args:
        rec [string]: The StreamPayload parsed record

    Returns:
        [dict]: Parsed JSON GHE message field
        [NoneType]: If no valid JSON object is found in the message field
    """
    json_pattern = re.compile(r'(?P<json_message>\{.*\})')
    match = re.search(json_pattern, rec['message'])

    if not match:
        return

    json_message = match.group('json_message')
    try:
        message_rec = json.loads(json_message)
    except ValueError:
        return

    return message_rec